Friday 19 August 2016

Chapter 5 Exercise 11, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

5.11 (Find numbers divisible by 5 or 6, but not both) Write a program that displays all the numbers from 100 to 200, ten per line, that are divisible by 5 or 6, but not both. Numbers are separated by exactly one space.

public class ProgrammingEx5_11 {
 
 public static void main(String[] args) {
  int itemCount = 0;
  for(int i=1; i<=1000; i++){
   if(i%5 == 0 ^ i%6 == 0){
    System.out.print(i + " ");
    itemCount +=1; 
     
    //print new line character after item 10th and reset the item count
    if(itemCount==10){
     System.out.println("\n");
     itemCount = 0;
    }
   }
  }
   
   
 
 }
 
}

No comments :

Post a Comment