Many Divisions - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for two numbers and displays the result of the division of these numbers along with the remainder of the division. If the second number entered is 0, the program should warn the user that division by 0 is not allowed. Additionally, if the first number entered is 0, the program will end with a farewell message. This exercise is useful for understanding how to handle exceptions and avoid errors like division by zero, which is an invalid operation in programming. To implement this exercise, if statements are used to check the values of the entered numbers and ensure that no invalid division is performed. The modulus operator is also used to calculate the remainder of the division. This kind of flow control is essential in applications that require checking conditions before performing mathematical operations, such as division.

 Category

Flow Control

 Exercise

Many Divisions

 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

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

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

  •  Several multiplication tables, (use do while)

    This C# exercise aims to create a program that displays multiplication tables from 2 to 6 using nested do...while loops. A do...while loop executes a block of ...

  •  Square

    This C# exercise aims to create a program that prompts the user to enter a number and a width, then displays a square with the given width, using...

  •  Break & continue

    This C# exercise aims to develop a program that displays the even numbers from 10 to 20, both inclusive, except for 16, using three different approaches...

  •  Rectangle V2

    This C# exercise aims to develop a program that asks the user for a number, a width, and a height, and then displays a rectangle with the provide...

  •  Repetitive structures

    This C# exercise aims to develop a program that prompts the user for two numbers and displays all the numbers between them (inclusive) three times using for, while...

  •  Digits in a number

    This C# exercise aims to teach how to develop a program that calculates the number of digits in a positive integer. The program uses an efficient technique by perform...