Write a Java program that displays all integers between 100 and 1,000 (inclusive) that are divisible by i) both 5 and 6 and ii) either 5 or 6, but not both. Display ten numbers per line, where consecutive numbers are separated by a single space. Clearly separate and mark the outputs for cases i) and ii).

Respuesta :

Answer:

//Begin class definition

public class Divisibility {

   

   //Begin main method

  public static void main(String [] args){

       

       //print some text

       //to show the numbers to be displayed

       System.out.println("========================================================");

      System.out.println("Integers between 100 and 1000 divisible by both 5 and 6");

       System.out.println("========================================================");

       

       //create an int variable

       //to hold the number of numbers printed

      int counter = 0;

       

       //create a loop that goes from 100 and 1000

       for(int i=100; i<=1000; i++){

           

           //if the number is divisibie by both 5 and 6

          if(i % 5 == 0 && i % 6 == 0){

               System.out.print(i + " ");

               counter++;

               if(counter > 0 && counter % 10 == 0){

                   System.out.println();

               }

           }

           

       }

       

       //print some text

       //to show the numbers to be displayed

       System.out.println("\n");

       System.out.println("======================================================================");

       System.out.println("Integers between 100 and 1000 divisible by either 5 or 6 but not both");

       System.out.println("======================================================================");

       

       

       //reset counter to 0

       counter = 0;

       

       //create a loop that goes from 100 and 1000

      for(int i=100; i<=1000; i++){

           

           //if the number is divisible by both 5 and 6

           //then skip

           if(i % 5 == 0 && i % 6 == 0){

               continue;

           }

           

           //if the number is divisible by 5 or 6

           //print the number,

           //increment counter

          if(i % 5 == 0 || i % 6 == 0){

               System.out.print(i + " ");

               counter++;

               //check if 10 numbers are already printed

               //if yes, print a new line

              if(counter > 0 && counter % 10 == 0){

                   System.out.println();

               }

           }

           

           

       }

       

       

   }   //End of main method

   

}  //End of class definition

Explanation:

The code above is written in Java. The actual lines of code are written in bold face while comments explaining the code are written in regular face.

The source code with the output is attached to this response. To execute this program, save the source code file as Divisible.java and run the file.

Ver imagen stigawithfun
Ver imagen stigawithfun
ACCESS MORE