Advertisement
Spocoman

10. Special Numbers

Oct 17th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.  
  8.         for (int i = 1; i <= n; i++) {
  9.             int sum = 0,
  10.                 number = i;
  11.             for (int j = 0; j < Integer.toString(i).length(); j++) {
  12.                 sum += number % 10;
  13.                 number /= 10;
  14.             }
  15.  
  16.             if (sum == 5 || sum == 7 || sum == 11) {
  17.                 System.out.printf("%d -> True\n", i);
  18.             } else {
  19.                 System.out.printf("%d -> False\n", i);
  20.             }
  21.         }
  22.     }
  23. }
  24.  
  25. OR:
  26.  
  27. import java.util.Scanner;
  28.  
  29. class Main {
  30.     public static void main(String[] args) {
  31.         Scanner scanner = new Scanner(System.in);
  32.         int n = Integer.parseInt(scanner.nextLine()),
  33.             number, sum;
  34.  
  35.         for (int i = 1; i <= n; i++) {
  36.             sum = 0;
  37.             number = i;
  38.             while (number > 0) {
  39.                 sum += number % 10;
  40.                 number /= 10;
  41.             }
  42.             System.out.printf("%d -> %s\n", i, (sum == 5 || sum == 7 || sum == 11) ? "True" : "False");
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement