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
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:
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# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…