Table of Contents
C# program to subtract two floating point number
In this article, we will discuss the concept of C# program to subtract two floating point number
In this post, we are going to learn how to write a program to subtract two floating – point numbers in C# language
Code to subtraction of two numbers in C#
Subtraction of two floating-point numbers
In this code, we are going to learn how to write a program to subtract two floating-point numbers using variables in C# programming language
Program 1
//Calculate difference of two floating point numbers using System; public class SubOfFloatNum { public static void Main(string[] args) { float num1=85.8f; float num2=56.9f;//declare and initialize variables Console.Write("The difference of two floats: "+num1+"+"+num2+"="); float subTot=num1-num2;//Calculate subtraction of two numbers Console.WriteLine(subTot);//display result } }
When the above code is executed, it produces the following result
The difference of two floats: 85.8-56.9=28.9
Subtraction of two floating-point numbers – Entered by user
In this example, the user is asked to enter two floating-point numbers. Then the subtraction of the these two floating-point numbers is calculated and displayed on the screen in C#
Program 2
//Calculate sum of two floating point numbers using System; public class SumOfFloatNum { //class declaration public static void Main(string[] args) { Console.WriteLine ("Enter float number to fNo"); //ask input from user for fNo float fNo=float.Parse(Console.ReadLine()); //Reading input value by user Console.WriteLine ("Enter second float number to lNo");//ask input from user for lNo float lNo=float.Parse(Console.ReadLine()); //Reading input value by user to lNo Console.Write("The difference of two floats: "+fNo+"-"+lNo+"="); float result=fNo-lNo;//calculate sum Console.WriteLine(result); //display result on the screen } } }
When the above code is executed, it produces the following result
Enter float number to fNo 120 Enter second float number to lNo 55 The difference of two floats: 120-55=65
In this code, the user asked to enter two floating-point numbers and the given numbers are stored in variables fNo and lNo respectively.
Then these two numbers are subtracted using the minus(-) operator and the result is stored in the result 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 of two numberĀ in Java language
Addition of two number in C language
Addition of two numberĀ in C++ language
Addition of two number in Python language