Site icon Codeforcoding

How to divide two floating-point number using function in C#

How to divide two floating-point number using function in C#

In this tutorial, we will discuss the concept of How to divide two floating-point number using function in C#

In this topic, we are going to learn how to write a program for dividing two floating-point numbers using function in C# programming language

 

Division of two numbers in C#

divide two floating-point numbers

Division of two floating-point numbers  using  function

In this program, the user declare and initialize two float variables num1 and num2. Then two numbers are divided using division(/) operator and assigned the result to third (division) variable

Program 1

//Write a program calculate division of two float numbers using function 
using System; 
public class Float_Div_Two_Num_Fun { //class declaration
public static float division_Num(float num1, float num2) { //function definition 
float division; //declare variable to assign result 
division=num1/num2; //calculate division of two numbers 
return division; //return value to main 
} 
public static void Main() { //main method 
float num1=24.2f; //declare and initialize float variable
float num2=4.4f; 
Console.WriteLine("The division of {0} and {1} is: {2}: ",num1,num2,division_Num(num1,num2)); //display result on the screen 
} }

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

The division of 24.2 and 4.4 is: 5.5:

 

Division of two floating-point numbers using  function-  Entered by user

In this program, the user is asked to enter two floatting-point values. These two numbers are stored in variables fNum and  lNum respectively .Then these two numbers are divided using division (/) operator
Then the division of fNum and lNum is evaluated  using  user  -defined  function and the result is stored in the divRes variable. finally the result is displayed on the screen

Program 2

// Write a program calculate divisionion of two numbers using function 
using System; 
public class Division_Of_Two_Num_Fun { 
    public static float divi_Num(float num1, float num2) { 
        float divRes; //declare float variable 
        divRes=num1/num2; //calculate division of the given numbers 
        return divRes;//return value to main method 
        } 
        public static void Main() { Console.Write("Enter first number : "); //Ask input from user 
        float fNum=Convert.ToSingle(Console.ReadLine()); //Reading the input 
        Console.Write("Enter number last number: "); //Ask input from user 
        float lNum=Convert.ToSingle(Console.ReadLine()); //Reading the input 
        Console.WriteLine("The division of {0} and {1} is: {2}: ",fNum,lNum,divi_Num(fNum,lNum)); } } //display result on the screen

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

Enter first number : 7.82
Enter number last number: 2.3
The division of 7.82 and 2.3 is: 3.4:

 

In the above program, two floating-pointt values7.82, 2.3 (get input from the user) are stored in fNum and lNum. Then fNum and lNum are divided using the division(/) operator.

Then the result is stored in another variable divRes.

Finally, the result is displayed on the screen using display statements in C#

 

Similar post

Java program to division  of  two numbers

C program to division of two numbers

C++ program to division two numbers

Python program to division two numbers

 

Java program to divide two floating-point  numbers

C program to divide two floating-point numbers

C++ program to divide two floating-point numbers

Python program to divide two floating-point numbers

C# program: function to calculate division of two numbers
PHP Adding Two Numbers  - PHP program
Exit mobile version