Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Uprajneniq;
- import java.util.Scanner;
- public class TopNumber {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read the input number
- int n = scanner.nextInt();
- // Iterate through the range [1, n]
- for (int i = 1; i <= n; i++) {
- // Check if the number is a top number
- if (isTopNumber(i)) {
- System.out.println(i);
- }
- }
- scanner.close();
- }
- // Method to check if a number is a top number
- public static boolean isTopNumber(int number) {
- // Check if the sum of digits is divisible by 8
- int sumOfDigits = 0;
- int num = number;
- while (num > 0) {
- sumOfDigits += num % 10;
- num /= 10;
- }
- if (sumOfDigits % 8 != 0) {
- return false;
- }
- // Check if the number has at least one odd digit
- num = number;
- while (num > 0) {
- int digit = num % 10;
- if (digit % 2 != 0) {
- return true;
- }
- num /= 10;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement