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.

  1. using System;

    • Imports the System namespace to use built-in functions like Console.WriteLine().
  2. namespace division_two_numbers

    • Defines a namespace named division_two_numbers to organize the class.
  3. class DivisionTwoNumbers

    • Declares a class named DivisionTwoNumbers, which contains the program logic.
  4. static void Main(String[] args)

    • The Main method is the entry point of the program.
  5. Declaring Variables:

    • int a, b; → Declares two integer variables to store user input.
  6. 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.
    • Similarly, the second number is read and stored in b.

  7. Division Operation:

    • int div = a / b;
      • Computes the quotient of a divided by b and stores it in div.
  8. Displaying the Result:

    • Console.WriteLine("Quotient of two numbers: " + div);
      • Prints the quotient to the console.

Important Consideration:

  • If b is zero, a divide-by-zero exception will occur, causing the program to crash.
  • To prevent this, an error-handling mechanism like try-catch or an if condition should be used to check if b is zero before performing division. Output
$ mcs DivisionTwoNumbers.cs
$ mono DivisionTwoNumbers.exe 
Enter first number:	60
Enter second number:	12
Quotient of two numbers: 5