Using do...while Loop Structure

The following Java program is a do…while Loop structure. In Java, do is a reserved word. As with the other repetition structures, a do…while statement can be either a simple or compound statement. If it is compound statement, enclose it between curly brackets. The expression is called loop condition. I will be using the JCreator IDE in developing the program. To start in this tutorial, first open the JCreator IDE, click new and paste the following code:
  1. import java.util.*;
  2.  
  3. public class doWhile
  4. {
  5.         static Scanner console = new Scanner(System.in);
  6.        
  7.         public static void main (String[] args)
  8.         {
  9.                 int num, temp, sum;
  10.                
  11.                 System.out.print("Enter a positive integer: ");
  12.                 num = console.nextInt();
  13.                 System.out.println();
  14.                
  15.                 temp = num;
  16.                
  17.                 sum = 0;
  18.                
  19.                 do
  20.                 {
  21.                         sum = sum + num % 10;
  22.                        
  23.                         num = num / 10;
  24.                        
  25.                 } while (num > 0);
  26.                
  27.                         System.out.println("The sum of the digits = " + sum);
  28.                
  29.                 if (sum % 3 == 0)
  30.                         System.out.println(temp + " is divisble by 3");
  31.                 else
  32.                                 System.out.println(temp + " is not divisble by 3");    
  33.                 if (sum % 3 == 0)
  34.                                                
  35.                                 System.out.println(temp + " is divisble by 9");
  36.                 else
  37.                                 System.out.println(temp + " is not divisble by 9");
  38.        
  39.         }
  40. }
Sample run: Enter a positive integer: 320 The sum of the digits = 5 320 is not divisble by 3 320 is not divisble by 9 This program works as follows: The statement  System.out.print("Enter a positive integer: "); prompts the user to enter a positive integer. The statement  num = console.nextInt(); store the next number in the variable num. The statement  temp = num; temp is equals to variable num. The statement  sum = 0; sets the value of variable sum to 0. The statement sum = sum + num % 10; extracts the last digit and add it to sum. The statement  num = num / 10; remove the last digit. The statement
  1.  do
  2.                 {
  3.                         sum = sum + num % 10;
  4.                        
  5.                         num = num / 10;
  6.                        
  7.                 } while (num > 0);<java> is the beginning and the end of the do..while loop.
The statements
  1.  if (sum % 3 == 0)
  2.                         System.out.println(temp + " is divisble by 3");
  3.                 else
  4.                                 System.out.println(temp + " is not divisble by 3");    
  5.                 if (sum % 3 == 0)
  6.                                                
  7.                                 System.out.println(temp + " is divisble by 9");
  8.                 else
  9.                                 System.out.println(temp + " is not divisble by 9");
Is the if…else condition where outputs the program if it is divisible by 3 or not, and divisible by 9 or not.

Add new comment