hpilo

Chapter2_SyntaxB_Ex5

Dec 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*  ==================================================
  4.           Chapter 2: Syntax section B- Loops
  5.  
  6.         Ex5: Identical digits (two digit number)
  7.     ===================================================
  8. */
  9.  
  10. public class MyProgram {
  11.     public static void main(String[] args) {
  12.  
  13.         //variables
  14.         int number;
  15.         boolean loop=false;     //represent identical digits
  16.         Scanner s=new Scanner(System.in);
  17.  
  18.         while(!loop){
  19.  
  20.             System.out.println("Enter a two digit number: ");
  21.             number=s.nextInt();     //we can assume valid input
  22.  
  23.             //if identical, break the loop
  24.             if(number%10==number/10)
  25.                 break;
  26.  
  27.  
  28.             /*
  29.                 //option 2:
  30.                 if(number%10==number/10)
  31.                     loop=true;
  32.             */
  33.  
  34.  
  35.         }
  36.  
  37.         System.out.println("Identical digits!\n***End***");
  38.  
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment