Table of Contents
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.