- On
- By
- 0 Comment
- Categories: Basic, C# basic, subtraction
Calculate subtraction of two floating point numbers in C#
Calculate subtraction of two floating point numbers in C#
In this tutorial, we will discuss the concept of Calculate subtraction of two floating point numbers in C#
In this topic, we are going to learn how to subtract two floating-point numbers using minus operator in C# programming language
Subtraction of two floating point numbers in C#
Subtraction of two floating-point numbers using variable
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 (sub) variable
Program 1
// Calculate subtraction of two floating-point numbers in C# using System; public class Subtract_Float { public static void Main(string[] args) { float num1=45.56f; //declaring and initializing first variable float num2=13.7f; //declaring and initializing second variable float sub=num1-num2; //subtracting two variable 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: 31.86
Subtraction of two floating-point numbers using user input
In this program, the user asked to enter two floating-point values. These two values are stored in variables n1 and n2 respectively Then two numbers are subtracted(num1-num2) using – operator and the result is stored in the sub variable
Program 2
// Calculate subtraction of two integers in C# program using System; public class Sub_Two_Num_Float{ public static void Main(string[] args) { float n1, n2, sub; Console.WriteLine("Find subtraction of two numbers: "); Console.Write("Input number to n1: "); n1=Convert.ToSingle(Console.ReadLine()); Console.Write("Input number to n2: "); n2=Convert.ToSingle(Console.ReadLine()); sub=n1-n2; Console.WriteLine ("The subtraction of given two floats: "+sub); Console.ReadKey(); } }
When the above code is executed, it produces the following result
Find subtraction of two numbers: Input number to n1: 56.78 Input number to n2: 45.67 The subtraction of given two floats: 11.11
In the above program, two floating-point values 56.78 and 45.67 (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
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