Many divisions Learn programming C#



Lesson:

Flow Control


Exercise:

Many divisions


Objetive:

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!


Code:

using System;
public class Exercise32
{
    public static void Main()
    {
        int num1, num2;
        do
        {
            Console.Write("First number? ");
            num1 = Convert.ToInt32(Console.ReadLine());

            if (num1 != 0)
            {
                Console.Write("Second number? ");
                num2 = Convert.ToInt32(Console.ReadLine());
                if (num2 == 0)
                {
                    Console.WriteLine("Cannot divide by 0");
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Division is {0}", num1 / num2);
                    Console.WriteLine("Remainder is {0}", num1 % num2);
                    Console.WriteLine();
                }
            }
        }
        while (num1 != 0);
        Console.WriteLine("Bye!");
    }
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.