Objective
Write a C# program that asks the user for two numbers and displays their division and remainder of the division. If 0 is entered as the second number, it will warn the user and end the program if 0 is entered as the first number. Examples:
First number? 10
Second number? 2
Division is 5
Remainder is 0
First number? 10
Second number? 0
Cannot divide by 0
First number? 10
Second number? 3
Division is 3
Remainder is 1
First number? 0
Bye!
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
int firstNumber, secondNumber; // Declaring variables to store the two numbers
// Asking the user to input the first number
Console.Write("First number? ");
firstNumber = int.Parse(Console.ReadLine()); // Reading the first number
// If the first number is 0, the program ends with a message
if (firstNumber == 0)
{
Console.WriteLine("Bye!"); // Displaying "Bye!" if the first number is 0
return; // Exiting the program
}
// Asking the user to input the second number
Console.Write("Second number? ");
secondNumber = int.Parse(Console.ReadLine()); // Reading the second number
// If the second number is 0, show an error message and end the program
if (secondNumber == 0)
{
Console.WriteLine("Cannot divide by 0"); // Informing the user that division by zero is not allowed
return; // Exiting the program
}
// If the second number is not 0, perform the division and display the result
int division = firstNumber / secondNumber; // Performing integer division
int remainder = firstNumber % secondNumber; // Calculating the remainder
// Displaying the results
Console.WriteLine($"Division is {division}"); // Showing the division result
Console.WriteLine($"Remainder is {remainder}"); // Showing the remainder result
}
}