Advertisement
mmayoub

while ex05 slide no 29

Jun 27th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package class170629;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class WhileSlide29Ex05 {
  6.  
  7.     public static void main(String[] args) {
  8.         /*
  9.          * קלוט מהמשתמש מספר והצג האם כל ספרותיו זהות
  10.          */
  11.  
  12.         // create a scanner
  13.         Scanner s = new Scanner(System.in);
  14.  
  15.         int n; // input: integer value
  16.  
  17.         do {
  18.             // ask for input
  19.             System.out.print("Enter a positive integer number: ");
  20.             // get and save input number
  21.             n = s.nextInt();
  22.         } while (n < 0); // be sure that n is positive
  23.  
  24.         // close scanner
  25.         s.close();
  26.  
  27.         int digit = n % 10; // calculated: first digit of the number
  28.         int tmp = n / 10; // calculated: other digits to be checked
  29.  
  30.         while (tmp > 0) {
  31.             if (tmp % 10 != digit) {
  32.                 break; // stop: in this case tmp>0
  33.             }
  34.  
  35.             tmp /= 10; // continue to next digit
  36.         }
  37.  
  38.         System.out.printf("digits of the number %d are %sthe same!", n,
  39.                 tmp > 0 ? "not " : "");
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement