Advertisement
myrdok123

05. Special Numbers

Jun 4th, 2023
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package L06_NestedLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P05_SpecialNumbers {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int number = Integer.parseInt(scanner.nextLine());
  11.  
  12.         //for -> 1111  до 9999 -> проверяваме дали конкретното число е специално
  13.  
  14.         for (int i = 1111; i <= 9999 ; i++) {
  15.  
  16.             //намираме всяка една цифра
  17.             int firstDigit = i / 1000;
  18.             int secondDigit = i / 100 % 10;
  19.             int thirdDigit = i / 10 % 10;
  20.             int fourthDigit = i % 10;
  21.  
  22.             // проверяваме дали числото е специално
  23.             boolean checkFirst = number % firstDigit == 0;
  24.             boolean checkSecond = secondDigit != 0 && number % secondDigit == 0;
  25.             boolean checkThird = thirdDigit != 0 && number % thirdDigit == 0;
  26.             boolean checkFourth = fourthDigit != 0 && number % fourthDigit == 0;
  27.  
  28.             //проверяваме дали имаме 4 пъти true
  29.  
  30.             if(checkFirst && checkSecond && checkThird && checkFourth){
  31.                 System.out.printf("%d ", i);
  32.             }
  33.  
  34.         }
  35.  
  36.  
  37.  
  38.  
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement