Write a C# program that prompts the user to input two numbers and divides them. Handle an exception when the user enters non-numeric values.

 


using System;

public class DivideNumbers
{
    public static double Divide(double numerator, double denominator)
    {
        if (denominator == 0)
        {
            throw new DivideByZeroException("Cannot divide by zero.");
        }

        return numerator / denominator;
    }

    public static void Main()
    {
        Console.WriteLine("Please enter the numerator:");
        double numerator = Convert.ToDouble(Console.ReadLine());

        Console.WriteLine("Please enter the denominator:");
        double denominator = Convert.ToDouble(Console.ReadLine());

        try
        {
            double result = Divide(numerator, denominator);
            Console.WriteLine("The result is: {0}", result);
        }
        catch (FormatException ex)
        {
            Console.WriteLine("Error: Invalid input.");
        }
        catch (DivideByZeroException ex)
        {
            Console.WriteLine("Error: Cannot divide by zero.");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex.Message);
        }
    }
}
Write a C# program that prompts the user to input two numbers and divides them. Handle an exception when the user enters non-numeric values. Write a C# program that prompts the user to input two numbers and divides them. Handle an exception when the user enters non-numeric values. Reviewed by Bhaumik Patel on 8:29 PM Rating: 5