C# Subtract two floating-point numbers using function – Example program
- Home
- Calculations
- C# Subtract two floating-point numbers using function – Example program
- On
- By
- 0 Comment
- Categories: Calculations, subtraction
C# Subtract two floating-point numbers using function – Example program
C# Subtract two floating-point numbers using function – Example program
In this tutorial, we will discuss the concept of C# Subtract two floating-point numbers using function – Example program
In this topic, we are going to learn how to subtract two floating-point numbers using function in C# programming language
Subtraction of two floating point numbers in C#
Subtraction of two floating-point numbers using function
In this program, the user declare and initialize two floating-point variables num1 and num2. Then two numbers are subtracted (num1 – num2) using minus (-) operator and then assign the result to third (tot_sub) variable
Program 1
//Write a program calculate subtraction of two float numbers using function using System; public class Sub_Two_Num_Fun { public static float sub_Num(float num1, float num2) { //function definition float tot_Sub; tot_Sub=num1-num2; //calculate sum of two numbers return tot_Sub; //return value } public static void Main() { float num1=65.8f; //declare and initialize float variable float num2=56.9f; Console.WriteLine("The sum 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 sum of 65.8 and 56.9 is: 8.900002:
Subtraction of two floating-point numbers using function – Entered by user
Program 2
In this program, the user is asked to enter two floating-point values. These two floating-point numbers are stored in variables n1 and n2 respectively .Then two numbers are subtracted using – operator and the result is stored in the def variable. finally the result is displayed on the screen
// Write a program calculate subtraction of two numbers using function using System; public class Sub_Two_Num_Fun { public static float sub_Num(float num1, float num2) { float def; //float variable def=num1-num2; //calculate difference of the given numbers return def;//return value to main method } public static void Main() { Console.Write("Enter number for n1: "); //Ask input from user float n1=Convert.ToSingle(Console.ReadLine()); //Reading the input Console.Write("Enter number for n2: "); //Ask input from user float n2=Convert.ToSingle(Console.ReadLine()); //Reading the input Console.WriteLine("The subtraction 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: 34.23 Enter number for n2: 23.45 The subtraction of 34.23 and 23.45 is: 10.78:
In the above program, two floating point values 34.23,23.45 (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 def.
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