In this example, I’ll show How to calculate sum of two integer entered by user.
Here we are taking the values from user and then performing the addition on the input numbers.
The reason we are using the Convert.ToInt32() function over the Console.ReadLine() function is because the Console.ReadLine() function receives the value as String, so to convert it into a number we are using the
Convert.ToInt32().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void Main(string[] args) { int num1, num2, sum; Console.WriteLine(" How to Calculate the sum of two numbers:"); Console.Write(" Input number 1: "); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write(" Input number 2: "); num2 = Convert.ToInt32(Console.ReadLine()); sum = num1 + num2; Console.Write(" {0} + {1} = {2}",num1,num2,sum); Console.ReadKey(); } |