Site icon Codeforcoding

C# divide two floating-point numbers

C# divide two floating-point numbers

In this tutorial, we will discuss the concept of C# divide two floating-point numbers

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

 

Division of two numbers in C#

divide two floating-point numbers

Division of two floating-point numbers

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

Program 1

// Calculate division of two floating-point numbers in C# program
using System; 
public class Div_Float{ 
    public static void Main(string[] args) { 
    float fNum=40.15f;//declare and initialize variables for num1
    float lNum=7.3f; //declare and initialize variables for num2
    float div=fNum/lNum; //Calculate division of two numbers 
        //assign the result to div variable
        Console.WriteLine ("The division of two floating point numbers: "+div); } } //display result on the screen

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

The division of two floating point numbers: 5.5

 

Division of two floating-point numbers- Entered by user

In this program, the user is asked to enter two floating-point values. These two floats are stored in variables num_One and num_Two respectively .Then these two floating-point numbers are divided using / operator
Then the division of num_One and num_Two is evaluated and the result is stored in the div variable. finally the result is displayed on the screen.

program 2

// Calculate division of two floating-point numbers in C# program
using System; 
public class Div_Of_Two_Num { 
    public static void Main(string[] args) { 
        float num_One, num_Two, div; //declare float varianles
    Console.WriteLine("Find division of two floating point numbers");
    Console.Write("Input first number: "); 
    //Ask input from the user
    num_One=Convert.ToSingle(Console.ReadLine()); 
    //Reading the input
    Console.Write("Input last number "); 
     //Ask input from the user
    num_Two=Convert.ToSingle(Console.ReadLine()); 
        //Reading the input
    div=num_One/num_Two;//Calculate division of given two numbers
Console.WriteLine ("The division of given two floating point numbers: "+div); 
Console.ReadKey(); } }

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

Find division of two floating point numbers
Input first number: 44.65
Input second number 9.5
The division of given two floating point numbers: 4.7

 

In the above program, two floating-point values 44.65,9.5 (get input from the user) are stored in num_One and num_Two. Then num_One and num_Two are divided using the division(/) operator for calculate division.

Then the result is stored in another variable div

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

 

Similar post

Java program to divide two numbers

C program to divide two numbers

C++ program to divide two numbers

Python program to divide 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 for division of two numbers
C# program: function to calculate division of two numbers
Exit mobile version