subtraction

Subtraction of two numbers in C# – Example program

Subtraction of two numbers in C# – Example program

In this tutorial, we will discuss the concept of  Subtraction of two numbers in C# – Example program

In this topic, we are going to learn how  to subtract two integer numbers using minus operator in C# programming language

Subtraction of two numbers

Subtraction of two numbers in C#

Subtraction of two numbers using variable

In this program, the user declare and initialize two integer variables num1 and num2. Then two numbers are subtracted second number(num2) from first number(num1) using minus operator and then assigned the result to third (sub) variable

Program 1

// Calculate subtraction of two integers in C# program 
using System;
public class Subtraction { 
    public static void Main(string[] args) {
        int num1=315; //Declaring and initializing first variable 
        int num2=130; //Declaring and initializing second variable 
        int sub=num1-num2; //subtracting two numbers and assign the value to third variable
        Console.WriteLine ("The subtraction of two numbers: "+sub); 
//Display result on the screen        
    } 
    
}

When the above code is executed, it produces the following result

The subtraction of two numbers: 185

 

Subtraction of two numbers using user input

Program 2

In this program, the user is asked to enter two integer values. These two integers are stored in variables n1 and n2 respectively .Then two numbers are subtracted using – operator and the result is stored in the sub variable. finally the result is displayed on the screen

// Calculate subtraction of two integers in C# program 
using System; 
public class Sub_Of_Two_Num { 
    public static void Main(string[] args) { 
        int n1, n2, sub; 
        Console.WriteLine("Find subtraction 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()); 
        sub=n1-n2; Console.WriteLine ("The subtraction of given two integers: "+sub);
        Console.ReadKey(); } }

When the above code is executed, it produces the following result

Find subtraction of two numbers:
Input number to n1: 890
Input number to n2: 789
The subtraction of given two integers: 101

 

In the above program, two integer values 890,789 (get input from the user) are stored in n1 and n2. Then n1 and n2 are subtracted using the minus(-) operator.

Then the result is stored in another variable sub.

Finally, the result is displayed on the screen using display statements in C#

 

Suggested for you

Subtract two numbers in Java language

Subtract of two numbers in C Language

Subtract of two numbers in C++ Language

Subtract two numbers in Python Language

 

Subtract of two numbers using method in Java language

Subtract of two numbers using function in C Language

Subtract of two numbers using function in C++ Language

Subtract of two numbers using function in Python Language

Function to Find sum of two floating-point numbers in C#
Calculate subtraction of two floating point numbers in C#
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago