Jumping Number in Java

A Jumping Number is a number in which each adjacent digit differs by only 1.

For example, 76789 is a Jumping Number.

Also note that all the one-digit numbers are considered to be Jumping Numbers.

Write a program in Java to input a positive integer and check whether it is a Jumping Number.

Display a suitable message accordingly.

import java.io.*;
class Jumping{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a positive integer: ");
        int num = Math.abs(Integer.parseInt(br.readLine()));
        int copy = num;
        boolean status = true;
        int diff = 0;
        while(num != 0){
            int d1 = num % 10;
            num /= 10;
            if(num != 0){
                int d2 = num % 10;
                if(Math.abs(d1 - d2) != 1){
                    status = false;
                    break;
                }
            }
        }
        if(status)
            System.out.println(copy + " is a jumping number.");
        else
            System.out.println(copy + " is NOT a jumping number.");
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.