-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisJumpingNumber.java
More file actions
38 lines (30 loc) · 891 Bytes
/
Copy pathisJumpingNumber.java
File metadata and controls
38 lines (30 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
public class isJumpingNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter X: ");
int X = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (int num = 0; num <= X; num++) {
if (isJumping(num)) {
sb.append(num).append(" ");
}
}
System.out.println("Jumping Numbers: " + sb.toString().trim());
sc.close();
}
static boolean isJumping(int n) {
if (n < 10) return true;
int prevDigit = n % 10;
n /= 10;
while (n > 0) {
int currDigit = n % 10;
if (Math.abs(prevDigit - currDigit) != 1) {
return false;
}
prevDigit = currDigit;
n /= 10;
}
return true;
}
}