Table of Contents
Different ways to Add two numbers in C#-Example program
In this article, we will discuss the concept of Different ways to Add two numbers in C#-Example program
In this post, we are going to learn how to write a program to add two numbers in different ways in C# language
Code to ways to Add two numbers in C#
Add two numbers in C#
In this code, we are going to learn how to write a program to add two numbers using initialized variables in C# programming language
Program 1
// Calculate addition of two integers in C# program using System; public class Sum_Num { public static void Main(string[] args) { int num1, num2, sum; num1=350; num2=230;//variable declaration and initialization sum=num1+num2;//calculate addition and assign the result to sum variable Console.WriteLine ("The sum of two numbers: "+sum);//display result } }
When the above program is executed, it produces the following result
The sum of two numbers: 580
Add two numbers in C# – Entered by user
In this example, the user is asked to enter two integer numbers. Then the sum of the these two integers is calculated and displayed on the screen
Program 2
// Calculate addition of two integers in C# program using System; public class Sum_Num { public static void Main(string[] args) { int num1, num2, sum; Console.WriteLine("Calculate sum of two num: "); Console.Write("Input number for num1: "); num1=Convert.ToInt32(Console.ReadLine()); Console.Write("Input number for num2: "); num2=Convert.ToInt32(Console.ReadLine()); sum=num1+num2; Console.WriteLine ("The sum of given two numbers: "+sum); Console.ReadKey(); } }
When the above program is executed, it produces the following result
Calculate sum of two num: Input number for num1: 55 Input number for num2: 60 The sum of given two numbers: 115
In this code, the user asked to enter two integers and the given integers are stored in variables num1 and num2 respectively.
Then these two numbers are added using the plus(+) operator and the result is stored in the sum variable.
Finally the display statement is used to print the addition of numbers
Program 3
// Calculate addition of two integers in C# program using System; public class Sum_Of_Two_Num { public static void Main(string[] args) { int first_Num, last_Num, sum_Of_Two; Console.WriteLine("Calculate sum of two numbers: "); Console.Write("Input number to first Number: "); first_Num=Convert.ToInt32(Console.ReadLine()); Console.Write("Input number to last number: "); last_Num=Convert.ToInt32(Console.ReadLine()); sum_Of_Two=first_Num+last_Num; Console.WriteLine (" {0} + {1} = {2}",first_Num, last_Num,sum_Of_Two); Console.ReadKey(); } }
When the above program is executed, it produces the following result
Calculate sum of two numbers: Input number to first Number: 345 Input number to last number: 456 345 + 456 = 801
Add two numbers in C# using function
In this example, the user is asked to enter two integer numbers. Then the sum of the these two integers is calculated and displayed on the screen using function in C#
Program 4
// Write a program calculate sum of two numbers using function using System; public class Sum_Two_Int { public static int sum_Num(int number1, int number2) { int total; total=number1+number2; return total; } public static void Main() { Console.Write("Enter number to num1: "); int num1=Convert.ToInt32(Console.ReadLine()); Console.Write("Enter number to num2: "); int num2=Convert.ToInt32(Console.ReadLine()); Console.WriteLine("The sum of {0} and {1} is: {2}: ",num1,num2,sum_Num(num1,num2)); } }
When the above program is executed, it produces the following result
Enter number to num1: 67 Enter number to num2: 77 The sum of 67 and 77 is: 144:
In this code, the user asked to enter two integers and the given integers are stored in variables num1 and num2 respectively.
Then these two numbers are added using the plus(+) operator and the result is stored in the total variable.
Finally the display statement is used to print the addition of numbers
Add two floating point numbers in C# using function
In this example, the user is asked to enter two floating point numbers. Then the sum of the these two floating-point numbers is calculated and displayed on the screen using function in C#
Program 5
//Calculate sum of two floating point numbers using System; public class SumOfFloat { public static void Main(string[] args) { Console.WriteLine ("Enter first float number for fNum"); float fNum=float.Parse(Console.ReadLine()); Console.WriteLine ("Enter second float number for lNum"); float lNum=float.Parse(Console.ReadLine()); Console.Write("The total of two floats: "+fNum+"+"+lNum+"="); float result=fNum+lNum; Console.WriteLine(result); } }
When the above program is executed, it produces the following result
Enter first float number for fNum 123.56 Enter second float number for lNum 235.67 The total of two floats: 123.56+235.67=359.23
In this code, the user asked to enter two floating-point numbers and the given numbers are stored in variables fNum and sNum respectively.
Then these two numbers are added using the plus(+) operator and the result is stored in the result variable.
Finally the display statement is used to print the addition of numbers
Suggested for you
Java program to add two numbers
C++ program to add two numbers
Python program to add two numbers
Write a program to sum of prime numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language
Write a program to sum of natural numbers 1 to n in Java language
Write a program to sum of prime numbers 1 to n in C language
Write a program to sum of prime numbers 1 to n in C++ language
Write a program to sum of prime numbers 1 to n in Python language