C# Program to divide two numbers
Program
using System;
namespace division_two_numbers
{
class DivisionTwoNumbers
{
static void Main(String[] args)
{
int a, b;
Console.Write("Enter first number:\t");
a = int.Parse(Console.ReadLine());
Console.Write("Enter second number:\t");
b = int.Parse(Console.ReadLine());
int div = a / b;
Console.WriteLine("Quotient of two numbers: " + div);
}
}
}
This C# program divides two numbers entered by the user and displays the quotient.
-
using System;- Imports the
Systemnamespace to use built-in functions likeConsole.WriteLine().
- Imports the
-
namespace division_two_numbers- Defines a namespace named
division_two_numbersto organize the class.
- Defines a namespace named
-
class DivisionTwoNumbers- Declares a class named
DivisionTwoNumbers, which contains the program logic.
- Declares a class named
-
static void Main(String[] args)- The
Mainmethod is the entry point of the program.
- The
-
Declaring Variables:
int a, b;→ Declares two integer variables to store user input.
-
Reading User Input:
-
Console.Write("Enter first number:\t");- Displays a prompt to enter the first number.
-
a = int.Parse(Console.ReadLine());- Reads the user input as a string, converts it to an integer, and stores it in
a.
- Reads the user input as a string, converts it to an integer, and stores it in
-
Similarly, the second number is read and stored in
b.
-
-
Division Operation:
int div = a / b;- Computes the quotient of
adivided byband stores it indiv.
- Computes the quotient of
-
Displaying the Result:
Console.WriteLine("Quotient of two numbers: " + div);- Prints the quotient to the console.
Important Consideration:
- If
bis zero, a divide-by-zero exception will occur, causing the program to crash. - To prevent this, an error-handling mechanism like
try-catchor anifcondition should be used to check ifbis zero before performing division. Output
$ mcs DivisionTwoNumbers.cs
$ mono DivisionTwoNumbers.exe
Enter first number: 60
Enter second number: 12
Quotient of two numbers: 5