Table of Contents
Csharp program to subtract two numbers
In this article, we will discuss the concept of Csharp program to subtract two numbers
In this post, we are going to learn how to write a program to subtract two numbers in C# language
Code to subtract two numbers in C#
Subtract two numbers in C#
In this code, we are going to learn how to write a program to subtract two numbers using variables in C# programming language
Program 1
// Calculate subtraction of two integers in C# program using System; public class Subtraction { public static void Main(string[] args) { int firstNum=315; //variable declaration and initailization for firstNum int lastNum=130; //variable declaration and initailization for lastNum int sub=firstNum-lastNum;//Subtract two numbers and assign the value to sub variable Console.WriteLine ("The subtraction of two numbers: "+sub); } }//display result on the screen //Console.WriteLine ("The subtraction of " +firstNum + " and " + lastNum + " : "+sub); } } //another way to display result on the screen
When the above code is executed, it produces the following result
The subtraction of two numbers: 185
Subtract two numbers in C# – Entered by user
In this example, the user is asked to enter two integer numbers. Then the subtraction of the these two integers is calculated and displayed on the screen in C#
Program 2
// Calculate subtraction of two integers in C# program using System; public class Sub_Of_Two_Num { public static void Main(string[] args) { int num_1, num_2, sub_res; Console.WriteLine("Find subtraction of two numbers: "); Console.Write("Input number to num_1: "); num_1=Convert.ToInt32(Console.ReadLine()); Console.Write("Input number to num_2: "); num_2=Convert.ToInt32(Console.ReadLine()); sub_res=num_1-num_2; Console.WriteLine ("The subtraction of given two integers: "+sub_res); Console.ReadKey(); } }
When the above code is executed, it produces the following result
Find subtraction of two numbers: Input number to num_1: 2345 Input number to num_2: 1234 The subtraction of given two integers: 1111
In this code, the user asked to enter two integers and the given integers are stored in variables num_1 and num_2 respectively.
Then these two numbers are subtracted using the minus(-) operator and the result is stored in the sub_Res variable.
Finally the display statement is used to print the subtraction of numbers
Suggested post
Sum of two integers using function in C#
Sum of two floating point numbers in C#
Subtract two number in Java language
Subtract two number in C language
Subtract two number in C++ language
Subtract two number in Python language
Addition two numberĀ in Java language
Addition two number in C language
Addition two numberĀ in C++ language
Addition two number in Python language