Saturday 11 June 2016

Chapter 3 Exercise 26, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

3.26 (Use the &&, || and ^ operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of this program:
Enter an integer: 10
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? true 


import java.util.Scanner;
 
public class ProgrammingEx3_26 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  System.out.print("Enter an integer:");
  int n = input.nextInt();
  boolean dby5 = false;
  boolean dby6 = false;
 
  if (n % 5 == 0) {
   dby5 = true;
  }
 
  if (n % 6 == 0) {
   dby6 = true;
  }
 
  System.out.println("Is 10 divisible by 5 and 6? " + (dby5 && dby6));
  System.out.println("Is 10 divisible by 5 or 6? " + (dby5 || dby6));
  System.out.println("Is 10 divisible by 5 or 6, but not both? "
    + (dby5 ^ dby6));
 
 }
 
}

No comments :

Post a Comment