Divide if not zero Learn programming Java

Lesson:

Flow Control


Exercise:

Divide if not zero


Objetive:

Write a java program to ask the user for two numbers and display their division if the second number is not zero; otherwise, it will display "I cannot divide".


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int number1, number2;

		System.out.print("Enter the first number:");
		number1 = Integer.parseInt(new Scanner(System.in).nextLine());
		System.out.print("Enter the second number:");
		number2 = Integer.parseInt(new Scanner(System.in).nextLine());

		if (number2 != 0)
		{
			System.out.printf("The result for %1$s / %2$s is %3$s" + "\r\n", number1, number2, number1 / number2);
		}

		if (number2 == 0)
		{
			System.out.println("No result");
		}
	}
}

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