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:
using System;
: This line imports theSystem
namespace, which contains many useful classes, including theConsole
class that we will use to interact with the console.namespace SumOfTwoNumbers
: This line declares a new namespace calledSumOfTwoNumbers
. A namespace is used to organize code and prevent naming collisions with other code in different namespaces.class Program
: This line declares a new class calledProgram
. TheMain
method will be defined inside this class.static void Main(string[] args)
: This line declares theMain
method, which is the entry point for the program. TheMain
method isstatic
, meaning it can be called without creating an instance of the class. Thevoid
return type means that theMain
method doesn’t return a value. Thestring[] args
parameter is an array of strings that can be passed as command-line arguments to the program.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 theWriteLine
method of theConsole
class.Console.WriteLine("Well, enter the first number: ");
: This line writes a prompt message asking the user to enter the first number.int num1 = Convert.ToInt32(Console.ReadLine());
: This line reads the user’s input as a string using theReadLine
method of theConsole
class, and converts it to an integer using theConvert.ToInt32
method. The result is stored in thenum1
variable.Console.WriteLine("Similarly,enter the second number: ");
: This line writes a prompt message asking the user to enter the second number.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 thenum2
variable.int sum = num1 + num2;
: This line calculates the sum ofnum1
andnum2
and assigns the result to thesum
variable.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 ofnum1
,num2
, andsum
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.