Divide If Not Zero (Using Else) - C# Programming Exercise

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 second number is not zero, it will perform the division of the two numbers and display the result on the screen. If the second number is zero, the program will display the message "I cannot divide". This exercise is useful for practicing the else structure, which allows you to handle the opposite case in a condition efficiently. Through this exercise, you will improve your ability to structure clearer and more readable control logic in C#.

Completing this exercise will enable you to create more robust programs that handle different conditions with conditional structures like if and else, preventing division by zero errors, a common issue in programming.

 Category

Flow Control

 Exercise

Divide If Not Zero (Using Else)

 Objective

Write a version of the previous program using 'else'. The program should ask the user for two numbers, and if the second number is not zero, it will display their division. Otherwise, it will display 'I cannot divide'.

 Write Your C# Exercise

using System; // Importing the System namespace to use Console functionalities

class Program
{
    // Main method where the program execution begins
    static void Main()
    {
        double firstNumber;  // Declaring a variable to store the first number entered by the user
        double secondNumber; // Declaring a variable to store the second number entered by the user

        // Asking the user to enter the first number and reading the input
        Console.Write("Enter the first number: ");
        firstNumber = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double

        // Asking the user to enter the second number and reading the input
        Console.Write("Enter the second number: ");
        secondNumber = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double

        // Using 'if' to check if the second number is not zero
        if (secondNumber != 0) // If the second number is not zero
        {
            // Performing the division and displaying the result
            Console.WriteLine("The result of division is: {0}", firstNumber / secondNumber); // Printing the division result
        }
        else // If the second number is zero, using 'else'
        {
            // Displaying an error message when division by zero is attempted
            Console.WriteLine("I cannot divide"); // Printing the error message
        }
    }
}

 Share this C# exercise

 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#.

  •  Greatest of three numbers

    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...

  •  Repeat until 0

    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...

  •  Repeat until 0 (Use Do While)

    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 re...

  •  While + Counter

    In this C# exercise, you will learn how to write a program that displays the numbers from 1 to 10 on the screen using a while loop. The while loop is a control...

  •  Multiplication table (use while)

    In this C# exercise, you will learn how to write a program that asks the user to enter a number and then displays its multiplication table using a while loop. The ...

  •  Odd numbers descending

    In this C# exercise, you will learn how to write a program that displays the odd numbers from 15 down to 7 using a while loop. The while loop is a control stru...