Basic Programming C# Examples

C# Program to Print the Sum of two numbers.

The Program in C# Program to Print the Sum of two numbers is given below:

using System;

namespace SumOfTwoNumbers
{
    class Program
    {
        static void Main(string[] args)
    
        {

            Console.WriteLine("Hello Codeauri Famuly, Enter the first number and Second number here to find their sum: ");
            Console.WriteLine("Well, enter the first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());
            
            Console.WriteLine("Similarly,enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());
            
            int sum = num1 + num2;
            Console.WriteLine("Okay, the sum of " + num1 + " and " + num2 + " is: " + sum);
        }
    }
}

Output:

Hello Codeauri Famuly, Enter the first number and Second number here to find their sum:
Well, enter the first number:
20550226
Similarly,enter the second number:
20560423
Okay, the sum of 20550226 and 20560423 is: 41110649

Pro-Tips💡

Here’s a step-by-step explanation of the above code:

  1. using System;: This line imports the System namespace, which contains many useful classes, including the Console class that we will use to interact with the console.
  2. namespace SumOfTwoNumbers: This line declares a new namespace called SumOfTwoNumbers. A namespace is used to organize code and prevent naming collisions with other code in different namespaces.
  3. class Program: This line declares a new class called Program. The Main method will be defined inside this class.
  4. static void Main(string[] args): This line declares the Main method, which is the entry point for the program. The Main method is static, meaning it can be called without creating an instance of the class. The void return type means that the Main method doesn’t return a value. The string[] args parameter is an array of strings that can be passed as command-line arguments to the program.
  5. Console.WriteLine("Hello Codeauri Famuly, Enter the first number and Second number here to find their sum: ");: This line writes a greeting message to the console using the WriteLine method of the Console class.
  6. Console.WriteLine("Well, enter the first number: ");: This line writes a prompt message asking the user to enter the first number.
  7. int num1 = Convert.ToInt32(Console.ReadLine());: This line reads the user’s input as a string using the ReadLine method of the Console class, and converts it to an integer using the Convert.ToInt32 method. The result is stored in the num1 variable.
  8. Console.WriteLine("Similarly,enter the second number: ");: This line writes a prompt message asking the user to enter the second number.
  9. int num2 = Convert.ToInt32(Console.ReadLine());: This line reads the user’s input for the second number and converts it to an integer, just like in step 7. The result is stored in the num2 variable.
  10. int sum = num1 + num2;: This line calculates the sum of num1 and num2 and assigns the result to the sum variable.
  11. Console.WriteLine("Okay, the sum of " + num1 + " and " + num2 + " is: " + sum);: This line writes the result of the calculation to the console, concatenating the values of num1, num2, and sum with string literals to create the desired output.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X