Table of Contents
In this article, we will discuss the concept of How to subtract two numbers using function in C#
In this post, we are going to learn how to write a program to subtract two numbers using user-defined function 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
//Write a program calculate subtraction of two numbers using function using System; public class Dif_Two_Num_Fun { //class declaration public static int sub_Num(int num1,int num2) { //user defined function with parameter int diff; //define a variable for assign result diff=num1-num2; //Calculate subtraction of given numbers return diff; //return to main method } public static void Main() { //main method int num1=300; //Declare and initialize variable int num2=150; //Declare and initialize variable Console.WriteLine("The difference 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 difference of 300 and 150 is: 150:
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
using System; public class Sub_Two_Num_Fun { //class declaration public static int sub_Num(int fNum, int sNum) { //function definition with parameter int difRes; //declare integer variable difRes=fNum-sNum; //calculate difference return difRes; //return to main method } public static void Main() { Console.Write("Enter number for num1: "); //ask input from the user int num1=Convert.ToInt32(Console.ReadLine()); //reading input from the user for num1 Console.Write("Enter number for num2: "); //ask input from the user int num2=Convert.ToInt32(Console.ReadLine()); //reading input from the user for num2 Console.WriteLine("The difference 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
Enter number for num1: 567 Enter number for num2: 345 The difference of 567 and 345 is: 222:
In this code, the user asked to enter two integers and the given integers are stored in variables num1 and num2 respectively.
Then these two numbers are subtracted using the minus(-) operator and the result is stored in the difRes 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.