Advertisement
CoineTre

JF-ExcMethods10.Top Number

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