Many divisions Learn programming Java

Lesson:

Flow Control


Exercise:

Many divisions 42


Objetive:

Write a java program to ask the user for two numbers and show their division and the remainder of the division. It will warn if 0 is entered as second number, and end if 0 is entered as first number:

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!");
	}
}