basic

C# program to subtract two floating point number

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 in C#

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

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.