Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
How to subtract two numbers using function in C#

Csharp program to subtract two numbers

Posted on May 16, 2022May 27, 2022

Table of Contents

  • Csharp program to subtract two numbers
    • Code to subtract two numbers in C#
      • Subtract two numbers in C#
      • Subtract two numbers in C# – Entered by user

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 in C#

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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes