How To Sum Two Number
In C# Using Console
In C# we can sum two numbers by taking input numbers from the users in terminal. In this blog I am going to discuss about how to take users input and sum operation.
In C# there are five operators available such as "+" for addition ,"-" for substraction ,"*" for multiplication ,"/" for division and "%" for modules ( reminder ). For addition we are going to use "+" operator.
We have tow different methods to add numbers in C# are
- Pre-defined Inputs
- Taking user input
Pre-defined Inputs Method ?
In this method the user is already defined the values of two numbers. Follow the Code .
1.Taking input values :- In this part we have to define variables .
For a=23, and b=35,
We are going to define variables by names a and b.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
/
namespace HACKINGSTAN
{
public class Program
{
public static void Main(string[] args)
{
//Code For Sum
int a=23;
int b=35;
}
}
}
2.Printing Elements (Answer):-
After defining variables then we have to print the the elements . To print we use "Console.WriteLine(a+b);" or "Console.WriteLine("The Sum of a+b is: {0} ", a+b); " .
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace HACKINGSTAN
{
public class Program
{
public static void Main(string[] args)
{
//Code For Sum
int a=23;
int b=35;
Console.WriteLine(a+b);
Console.ReadLine();
}
}
}
Output :-
Output is shown on the picture .
Taking user input :-
We can ask input or values of a and b from the user by using this method. To use this method follow the steps that written below .
1. Ask The User For Input :
We can ask the user to enter the values of a and b by using "Console.WriteLine("Enter The First Number: ");" .
2. Storing input in variable :
After getting inputs we have to store the input in a variable . For numbers we use int datatype of variable. If a= first number and b=second number then we have to declare the first number as " int a; " .
3. Change The Variable type to Integers :
After storing the input we have to change the type of the input, because every input of variable is stored in String(Character) . To change String Value To Inter Use
" a=Convert.ToInt32(Console.ReadLine()); ".
For second number b repeat the same process from 1-3 method.
4. Printing The Result In Console :
We can print the results by using two methods. Direct Printing method by using "Console.WriteLine(a+b);" . Second Method Is based On parentheses of 0 and 1 as "Console.WriteLine("The Value Of a+b is : {0},a+b);" .
Output :-
Output is shown in the below image .
Code For This Method :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace HACKINGSTAN
{
public class Program
{
public static void Main(string[] args)
{
//Code For Sum
Console.WriteLine("Enter The First Number");
int a;
a=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter The second Number");
int b;
b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The Sum Of a and b : {0}", a+b);
Console.ReadLine();
}
}
}
For More Tutorials and Free Course Follow The Blog Every Day. Support Our Team @Hackingstan at Instagram By Following






