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

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.