C# Programs to perform basic arithmetic operations
The basic arithmetic operations are addition, subtraction, multiplication and division. Here are the C# programs which can be used to perform the basic arithmetic operations on two numbers belonging to 'int' datatype.
Addition
Here is the C# program to perform addition of two integers, its explanation and its sample outputs.Program
using System;
namespace CsharpPrograms
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int addition;
Console.Write("\n Enter the first number : ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("\n Enter the second number : ");
b=Convert.ToInt32(Console.ReadLine());
addition=a+b;
Console.Write("\n The result of addition : "+addition);
Console.ReadLine();
}
}
}Explanation
Inside the Main() function, the first thing to do is the declaration of two 'int' variables used to store the integers to be added. Then, another 'int' variable used to store the result of addition should be declared. After that, the numbers to be added are to be scanned. Then, the values of two 'int' variables should be added and the result should be stored in the third 'int' variable. After that, the value in the third 'int' variable should be printed. Hence, the addition of two integers. In case you want to perform addition of two non integer numbers, change the datatypes of the three variables declared.Output
Run 1
Enter the first number : 2
Enter the second number : 3
The result of addition : 5
Run 2
Enter the first number : 59
Enter the second number : 49
The result of addition : 108Subtraction
Here is the C# program to perform subtraction of two integers, its explanation and its sample outputs.Program
using System;
namespace CsharpPrograms
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int subtraction;
Console.Write("\n Enter the first number : ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("\n Enter the second number : ");
b=Convert.ToInt32(Console.ReadLine());
subtraction=a-b;
Console.Write("\n The result of subtraction : "+subtraction);
Console.ReadLine();
}
}
}Explanation
Inside the Main() function, the first thing to do is the declaration of two 'int' variables used to store the integers to be subtracted. Then, another 'int' variable used to store the result of subtraction should be declared. After that, the numbers to be subtracted are to be scanned. Then, the values of two 'int' variables should be subtracted and the result should be stored in the third 'int' variable. After that, the value in the third 'int' variable should be printed. Hence, the subtraction of two integers. In case you want to perform subtraction of two non integer numbers, change the datatypes of the three variables declared.Output
Run 1
Enter the first number : 3
Enter the second number : 2
The result of subtraction : 1
Run 2
Enter the first number : 59
Enter the second number : 49
The result of subtraction : 10Multiplication
Here is the C# program to perform multiplication of two integers, its explanation and its sample outputs.Program
using System;
namespace CsharpPrograms
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int multiplication;
Console.Write("\n Enter the first number : ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("\n Enter the second number : ");
b=Convert.ToInt32(Console.ReadLine());
multiplication=a*b;
Console.Write("\n The result of multiplication : "+multiplication);
Console.ReadLine();
}
}
}Explanation
Inside the Main() function, the first thing to do is the declaration of two 'int' variables used to store the integers to be multiplied. Then, another 'int' variable used to store the result of multiplication should be declared. After that, the numbers to be multiplied are to be scanned. Then, the values of two 'int' variables should be multiplied and the result should be stored in the third 'int' variable. After that, the value in the third 'int' variable should be printed. Hence, the multiplication of two integers. In case you want to perform multiplication of two non integer numbers, change the datatypes of the three variables declared.Output
Run 1
Enter the first number : 3
Enter the second number : 3
The result of multiplication : 9
Run 2
Enter the first number : 60
Enter the second number : 20
The result of multiplication : 1200Division
Here is the C# program to perform division of two integers, its explanation and its sample outputs.Program
using System;
namespace CsharpPrograms
{
class Program
{
static void Main(string[] args)
{
int a;
int b;
int division;
Console.Write("\n Enter the first number : ");
a=Convert.ToInt32(Console.ReadLine());
Console.Write("\n Enter the second number : ");
b=Convert.ToInt32(Console.ReadLine());
division=a/b;
Console.Write("\n The result of division : "+division);
Console.ReadLine();
}
}
}Explanation
Inside the Main() function, the first thing to do is the declaration of two 'int' variables used to store the integers to be divided. Then, another 'int' variable used to store the result of division should be declared. After that, the numbers to be divided are to be scanned. Then, the values of two 'int' variables should be divided and the result should be stored in the third 'int' variable. After that, the value in the third 'int' variable should be printed. Hence, the division of two integers. In case you want to perform division of two non integer numbers, change the datatypes of the three variables declared.Output
Run 1
Enter the first number : 3
Enter the second number : 3
The result of division : 1
Run 2
Enter the first number : 60
Enter the second number : 20
The result of division : 3
The beginners of C# should try to note the minute differences between all the above programs so that they can get a basic idea of C# programming like how to scan, print etc,.