Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C# program to subtract two floating point number

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

Posted on May 20, 2022May 20, 2022

Table of Contents

  • Subtraction of two floating-point numbers using function in C#
    • Code to subtract two numbers in C#
      • Subtract two floating-point numbers using function
      • Subtract two floating-point numbers using function – Entered by user

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

 

 

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes