- On
- By
- 0 Comment
- Categories: addition, Basic, Calculations
Calculate sum of two numbers – Example program in C#
Calculate sum of two numbers in C# – Example program in C#
In this tutorial, we will discuss the concept of Calculate sum of two numbers – Example program in C#
In this topic, we are going to learn how to add two integer numbers using plus operator in C# programming language
Sum of two numbers using variable
Program 1
// Calculate sum of two integers in C# program using System; public class Addition { public static void Main(string[] args) { int num1=35; int num2=30; int sum=num1+num2; Console.WriteLine ("The sum of two numbers: "+sum); } }
When the above code is executed, it produces the following result
The sum of two numbers: 65
Sum of two numbers using user input
Program 2
In this program, the user asked to enter two integer values. these two integers are stored in variables n1 and n2 respectively then two numbers are added using + operator and the result is stored in the sum variable
// Calculate addition of two integers in C# program using System; public class Sum_Of_Two_Num { public static void Main(string[] args) { int n1, n2, sum; Console.WriteLine("Find sum of two numbers: "); Console.Write("Input number to n1: "); n1=Convert.ToInt32(Console.ReadLine()); Console.Write("Input number to n2: "); n2=Convert.ToInt32(Console.ReadLine()); sum=n1+n2; Console.WriteLine ("The Addition of given two integers: "+sum); Console.ReadKey(); } }
When the above code is executed, it produces the following result
Find sum of two numbers: Input number to n1: 123 Input number to n2: 234 The Addition of given two integers: 357
In the above program, two integer values 123,234 (get input from the user) are stored in n1 and n2. Then n1 and n2 are added using the plus(+) operator.
Then the result is stored in another variable sum.
Finally, the result is displayed on the screen using display statements in C#
Sum of two floating point numbers using user input
In this program, the user asked to enter two floating point values. these two floats are stored in variables num1 and num2 respectively. Then two numbers are added using + operator and the result is stored in the result variable
Program 3
//Calculate sum of two floating point numbers using System; public class SumOfFloat { public static void Main(string[] args) { Console.WriteLine ("Enter two floating point numbers"); float num1=float.Parse(Console.ReadLine()); float num2=float.Parse(Console.ReadLine()); Console.Write("The sum of two floats: "+num1+"+"+num2+"="); float result=num1+num2; Console.WriteLine(result); } }
When the above code is executed, it produces the following result
Enter two floating point numbers 34.5 12.6 The sum of two floats: 34.5+12.6=47.1
In the above program, two floating-point values 34.5,12.6 (get input from the user) are stored in num1 and num2. Then num1 and num2 are added using the plus(+) operator.
Then the result is stored in another variable result.
Finally, the result is displayed on the screen using display statements in C#
Suggested for you
Operator in Java language
Data types and variable in Java language
Similar post
Sum of two numbers in C language
Sum of two numbers in C++ language
Sum of two numbers in Python language
Sum of two numbers in Java language without + operator
Sum of two numbers in Java language using bitwise operator
Sum of two numbers in C language without + operator
Sum of two numbers in C language using bitwise operator
Calculate addition of two numbers using method in Java language
Calculate addition of two numbers using function in C language
Calculate addition of two numbers using function in C++ language
Calculate addition of two numbers using function in Python language