Objective
Write a C# program to prompt the user for two numbers and display their division. Errors should be caught using "try..catch"
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace, which contains fundamental classes like Console
class Program // Define the Program class
{
static void Main() // The entry point of the program
{
try // Start the try block to catch potential errors
{
// Ask the user for the first number (numerator)
Console.Write("Enter the first number (numerator): ");
int num1 = int.Parse(Console.ReadLine()); // Read and convert the first input to an integer
// Ask the user for the second number (denominator)
Console.Write("Enter the second number (denominator): ");
int num2 = int.Parse(Console.ReadLine()); // Read and convert the second input to an integer
// Perform the division and display the result
int result = num1 / num2; // Divide the first number by the second
Console.WriteLine($"The result of {num1} divided by {num2} is: {result}"); // Output the result
}
catch (DivideByZeroException) // Catch the specific exception when trying to divide by zero
{
// Handle the divide by zero error
Console.WriteLine("Error: Cannot divide by zero."); // Display an error message
}
catch (FormatException) // Catch the format exception if the user doesn't input valid integers
{
// Handle the invalid input error
Console.WriteLine("Error: Please enter valid integers."); // Display an error message
}
catch (Exception ex) // Catch any other unexpected exceptions
{
// Handle any general exception
Console.WriteLine($"An unexpected error occurred: {ex.Message}"); // Display a general error message
}
}
}
More C# Programming Exercises of Flow Control
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...
In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...
In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...
In this C# exercise, you will learn how to modify the previous program using the else control structure. The program will ask the user for two numbers, and if the sec...
In this C# exercise, you will learn how to write a program that prompts the user to enter three numbers and displays the greatest one. The program will use conditional struc...
In this C# exercise, you will learn how to write a program that asks the user to enter a number "x" and displays the result of multiplying it by 10. The program will keep as...