Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TopNumber {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int number = Integer.parseInt(sc.nextLine());
- sc.close();
- topNumbers(number);
- }
- static void topNumbers(int n){
- for (int i = 1; i <= n; i++){
- if (divisibleBy8(sumDigits(i)) && oddDigit(i)){
- System.out.println(i);
- }
- }
- }
- static int sumDigits(int n){
- int sum = 0;
- int t = n;
- while (t != 0){
- sum += t % 10;
- t /= 10;
- }
- return sum;
- }
- static boolean divisibleBy8(int n){
- if(n % 8 == 0)
- return true;
- else
- return false;
- }
- static boolean oddDigit(int n){
- boolean odd = false;
- int t = n;
- while (t != 0){
- if ((t % 10) % 2 != 0){
- odd = true;
- }
- t /= 10;
- }
- return odd;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment