Table of Contents
CSharp program to divide two numbers
In this article, we will discuss the concept of CSharp program to divide two numbers
In this post, we are going to learn how to write a program to divide two numbers (one by one) in C# language
Code to divide two numbers in C#
division of two numbers in C#
In this code, we are going to learn how to write a program to divide two numbers using variable(initialization) in C# programming language
Program 1
// Calculate division of two integers in C# program using System; public class Division { public static void Main(string[] args) { int fNum=35; //declare and initialize variables for fNum int lNum=7; //declare and initialize variables for lNum int div=fNum/lNum; //Calculate division of two numbers //assign the result to div variable Console.WriteLine ("The division of two numbers: "+div); } } //display result on the screen
When the above code is executed it produces the following result
The division of two numbers: 5
Division of two numbers in C# – Entered by user
In this example, the user is asked to enter two integer numbers. Then the division of the these two integers is calculated and displayed on the screen in C#
Program 2
// Calculate division of two integers in C# program using System; public class Div_Of_Two_Num { public static void Main(string[] args) { int numOne, numTwo, div; //declare integer variables Console.WriteLine("Find division of two numbers"); Console.Write("Input first number: "); //Ask input from the user numOne=Convert.ToInt32(Console.ReadLine()); //Reading the input Console.Write("Input second number "); //Ask input from the user numTwo=Convert.ToInt32(Console.ReadLine()); //Reading the input div=numOne/numTwo; //Calculate division of given two numbers Console.WriteLine ("The division of given two integers: "+div); Console.ReadKey(); } }
When the above code is executed it produces the following result
Find division of two numbers Input first number: 1250 Input second number 50 The division of given two integers: 25
In this code, the user asked to enter two integers and the given integers are stored in variables numOne and numTwo respectively.
Then those two numbers are divided using the division(/) operator and the result is stored in the div variable.
Finally the display statement is used to print the division of numbers
division of two numbers in C# using function
In this example, the user is asked to enter two integer numbers. Then the division of the these two integers is calculated and displayed on the screen using user-defined function in C# language
Program 3
// Write a program find division of two numbers using function using System; public class DivTwo_NumFun { public static int divtwoNum(int num1, int num2) { //User defined function definition with parameter int divResult; //declare integer variable for result divResult=num1/num2; //calculate division //assign the result to third variable return divResult; } //return result to main method public static void Main() { Console.Write("Enter first number: "); int n1=Convert.ToInt32(Console.ReadLine()); //get input from the user for n1 Console.Write("Enter last number: "); 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,divtwoNum(n1,n2)); } } //display result on the screen
When the above code is executed it produces the following resultv
Enter first number: 120 Enter last number: 6 The division of 120 and 6 is: 20:
Suggested post
Java program to divide two number
C program to divide two number
C++ program to divide two number
Python program to divide two number
java program to find quotient and remainder
C program to find quotient and remainder