calculations

Subtraction of two floating-point numbers using function in C#

Subtraction of two floating-point numbers using function in C#

In this article, we will discuss the concept of Subtraction of two floating-point numbers using function in C#

In this post, we are going to learn how to write a program to subtract two floating-point numbers using user-defined function in C# language

Code to subtract two numbers in C#

Subtract two floating-point numbers using function

In this code, we are going to learn how to write a program to subtract two foating-point numbers using function in C# programming language

Program 1

//Write a program calculate subtraction of two floating-point numbers using function
using System; 
public class Sub_Two_Float_Num_Fun { 
    public static float sub_Num(float num1, float num2) {
        //function definition 
        float sub; 
        sub=num1-num2; //calculate subtraction of two numbers 
        return sub; 
    //return value 
    } 
    public static void Main() {
        float n1=68.8f; //declare and initialize float variables
    float n2=53.7f; 
    Console.WriteLine("The subtraction of {0} and {1} is: {2}: ",n1,n2,sub_Num(n1,n2)); } }//display result

When the above code is executed, it produces the following result

The subtraction of 68.8 and 53.7 is: 15.1:

 

Subtract two floating-point numbers using function – 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

using System; 
public class Sub_Two_Num_Fun { //class declaration
    public static float sub_Num(float f_Num, float s_Num) { //function definition with parameter
        float dif_Res; //declare integer variable
        dif_Res=f_Num-s_Num; //calculate difference
        return dif_Res; //return to main method
        
    }
    public static void Main() { 
        Console.Write("Enter number for f_Num: "); 
        //ask input from the user
    float f_Num=Convert.ToInt32(Console.ReadLine()); 
    //reading input from the user for f_Num
    Console.Write("Enter number for s_Num: "); 
    //ask input from the user
    float s_Num=Convert.ToInt32(Console.ReadLine()); 
     //reading input from the user for s_Num
    Console.WriteLine("The difference of {0} and {1} is: {2}: ",f_Num,s_Num,sub_Num(f_Num,s_Num)); } }
    //display result on the screen

When the above code is executed, it produces the following result

Enter number for f_Num: 432
Enter number for s_Num: 321
The difference of 432 and 321 is: 111:

 

In this code, the user asked to enter two floating-point numbers and the given numbers are stored in variables f_Num and s_Num respectively.

Then these two numbers are subtracted using the minus(-) operator and the result is stored in the dif_Res 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.