SHARE
TWEET

TopNumber

a guest Feb 27th, 2020 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  9.         for (int i = 1; i <= n; i++) {
  10.  
  11.             if (sumIsDivisibleByEight(i) && hasOneOddDigit(i)) {
  12.                 System.out.println(i);
  13.             }
  14.         }
  15.     }
  16.  
  17.     private static boolean sumIsDivisibleByEight(int number) {
  18.         int sum = 0;
  19.         String numberAsString = "" + number;
  20.         for (int i = 0; i < numberAsString.length(); i++) {
  21.             int currentDigit = Character.getNumericValue(numberAsString.charAt(i));
  22.             sum += currentDigit;
  23.         }
  24.         return sum % 8 == 0;
  25.     }
  26.  
  27.     private static boolean hasOneOddDigit(int number) {
  28.         String numberAsString = "" + number;
  29.         for (int i = 0; i < numberAsString.length(); i++) {
  30.             int currentDigit = Character.getNumericValue(numberAsString.charAt(i));
  31.             if (currentDigit % 2 != 0) {
  32.                 return true;
  33.             }
  34.         }
  35.         return false;
  36.     }
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top