Calculations

C# program to Subtract two numbers using function – Example program

C# program to Subtract two numbers using function – Example program

In this tutorial, we will discuss the concept of  C# program to Subtract two numbers using function – Example program

In this topic, we are going to learn how  to subtract two numbers using function 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 variables num1 and num2. Then two numbers are subtracted  (num1 – num2) using minus (-) operator and then assign the result to third (diff) variable

Program 1

//Write a program calculate subtraction of two numbers using function
using System; 
public class Dif_Two_Num_Fun { //class definition
    public static int sub_Num(int num1,int num2) { //user defined function with argument
        int diff; 
        diff=num1-num2; //Calculate difference of given numbers
        return diff; 
        
    } public static void Main() { 
        int num1=450; //Declare and initialize variable
        int num2=150; 
        Console.WriteLine("The subtraction of {0} and {1} is: {2}: ",num1,num2,sub_Num(num1,num2)); } }
        //Display result on the screen

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

The subtraction of 450 and 150 is: 300:

 

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 difRes variable. finally the result is displayed on the screen

// Write a program calculate subtract of two numbers using function
using System; 
public class Sub_Two_Num_Fun { 
    public static int sub_Num(int num1, int num2) { //function definition
        int difRes; //declare integer variable
        difRes=num1-num2; //calculate difference
        return difRes; 
        
    }
    public static void Main() { 
        Console.Write("Enter number for n1: "); 
    int n1=Convert.ToInt32(Console.ReadLine()); 
    //get input from the user for n1
    Console.Write("Enter number for n2: "); 
    int n2=Convert.ToInt32(Console.ReadLine()); 
    //get input from the user for n2
    Console.WriteLine("The difference of {0} and {1} is: {2}: ",n1,n2,sub_Num(n1,n2)); } }
    //display result on the screen

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

Enter number for n1: 4567
Enter number for n2: 3456
The difference of 4567 and 3456 is: 1111:

 

In the above program, two integer values 4567,3456 (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 defRes.

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

 

Suggested for you

Subtraction of two numbers in Java language

Subtraction of two numbers in C Language

Subtraction of two numbers in C++ Language

Subtraction of two numbers in Python Language

 

Subtraction of two numbers using method in Java language

Subtraction of two numbers using function in C Language

Subtraction of two numbers using function in C++ Language

Subtraction of two numbers using function in Python Language

Calculate subtraction of two floating point numbers in C#
C# Subtract two floating-point  numbers using function - Example program
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago