Many divisions Learn programming Java

Lesson:

Flow Control


Exercise:

Many divisions


Objetive:

Write a java 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:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int num1, num2;
		do
		{
			System.out.print("First number? ");
			num1 = Integer.parseInt(new Scanner(System.in).nextLine());

			if (num1 != 0)
			{
				System.out.print("Second number? ");
				num2 = Integer.parseInt(new Scanner(System.in).nextLine());
				if (num2 == 0)
				{
					System.out.println("Cannot divide by 0");
					System.out.println();
				}
				else
				{
					System.out.printf("Division is %1$s" + "\r\n", num1 / num2);
					System.out.printf("Remainder is %1$s" + "\r\n", num1 % num2);
					System.out.println();
				}
			}
		} while (num1 != 0);
		System.out.println("Bye!");
	}
}

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