Calculations

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 numbersv

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

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

2 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

4 weeks ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

4 weeks ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago