Print all jumping numbers smaller than or equal to x.
Input: x = 105
Output: 0 1 2 3 4 5 6 7 8 9 10 12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 98 101
Note: A number is called as a jumping number if all adjacent digits in it differ by 1. The difference between ‘9’ and ‘0’ is not considered as 1.
public class JumpingNumber {
public static boolean jumpingNumer(int num) {
while (num > 9) {
int diff = num % 10;
num = num / 10;
int diff1 = num % 10;
if (Math.abs(diff - diff1) != 1)
return false;
}
return true;
}
public static void main(String[] args) {
int n = 105;
for (int i = 0; i <= n; i++) {
if (jumpingNumer(i)) {
System.out.print(i + " ");
}
}
}
}
Thanks and Regards,
Solution provided by Nilmani Prashanth

