Wednesday, October 30, 2013

2.04 - Divide if not zero using else

/*
 * Autor: Juan Antonio Ripoll
 * Date: 30/10/13
 * 
 * Write a JAVA program to ask the user for two numbers, and show 
 * their division if the second number is not zero; otherwise, 
 * it will display "I cannot divide 
 * */

import java.util.Scanner;

public class Main {

 public static void main(String[] args) {
  Scanner in = new Scanner(System.in);
  
  System.out.print("Enter the first number: ");
  int number1 = in.nextInt();
  
  System.out.print("Enter the second number: ");
  int number2 = in.nextInt();
  
  
  if (number2!=0)
   System.out.print(number1 + " / " + number2 + " = " 
      + number1/number2);
  else
   System.out.print("I cannot divide.");
 }
}

No comments:

Post a Comment