Didart

Top Number

Oct 8th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TopNumber {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         for (int number = 1; number <= n; number++) {
  9.  
  10.             if (isSumDigitsDivisible(number) && holdsOddDigit(number)) {
  11.                 System.out.println(number);
  12.             }
  13.         }
  14.     }
  15.  
  16.     public static boolean isSumDigitsDivisible(int number) {
  17.         int sum = 0;
  18.         while (number > 0) {
  19.             int lastDigit = number % 10;
  20.             sum += lastDigit;
  21.             number = number / 10;
  22.         }
  23.         return sum % 8 == 0;
  24.     }
  25.  
  26.     private static boolean holdsOddDigit(int number) {
  27.         while (number > 0) {
  28.             int lastDigit = number % 10;
  29.             if (lastDigit % 2 == 1) {
  30.                 return true;
  31.             }
  32.             number = number / 10;
  33.         }
  34.         return false;
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment