Codeforcoding

C# program: function to calculate division of two numbers

C# program: function to calculate division of two numbers

In this tutorial, we will discuss the concept of C# program: function to calculate division of two numbers

In this topic, we are going to learn how to write a program for dividing two numbers (one by one) using function  in C# programming language

 

Division of two numbers in C#

C# program for division of two numbers
division of two numbers

Division of two numbers

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

Program 1

//Write a program calculate divsion of two numbers using function
using System; 
public class Div_Two_Num_Fun { //class definition 
public static int div_Num(int num1,int num2) { //user defined function 
int div; 
div=num1/num2; //Calculate division of given numbers 
return div; 
} 
public static void Main() { 
    int num1=450; //Declare and initialize variable 
    int num2=50; 
    Console.WriteLine("The division of {0} and {1} is: {2}: ",num1,num2,div_Num(num1,num2)); } } //Display result on the screen

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

The division of 450 and 50 is: 9:

 

Division of two numbers  –  Entered by user

In this program, the user is asked to enter two integer values. These two integers are stored in variables n1 and n2 respectively .Then these two numbers are divided using division (/) operator
Then the division of n1 and n2 is evaluated and the result is stored in the divRes variable. finally the result is displayed on the screen

Program 2

// Write a program calculate division of two numbers using function 
using System; 
public class Div_Two_Num_Fun { 
    public static int div_Num(int num1, int num2) { //function definition 
    int divRes; //declare integer variable 
    divRes=num1/num2; //calculate multiplication 
    return divRes; } 
    public static void Main() { 
        Console.Write("Enter number for n1: "); 
        int n1=Convert.ToInt32(Console.ReadLine()); //get input from the user for n1 
        Console.Write("Enter number for n2: "); 
        int n2=Convert.ToInt32(Console.ReadLine()); //get input from the user for n2 
        Console.WriteLine("The division of {0} and {1} is: {2}: ",n1,n2,div_Num(n1,n2)); } } //display result on the screen

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

Enter number for n1: 125
Enter number for n2: 25
The multiplication of 125 and 25 is: 5:

 

In the above program, two integer values 125, 25 (get input from the user) are stored in n1 and n2. Then n1 and n2 are divided using the division(/) operator for calculate division.

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 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# divide two floating-point numbers
How to divide two floating-point number using function in C#
Exit mobile version