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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.