Stepping Numbers | A Java program to find all the Stepping Number in [ n , m ] ; This Method checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is consider as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if it is a stepping number . ; Driver code ; Display Stepping Numbers in the range [ n , m ]
class Main { public static boolean isStepNum ( int n ) { int prevDigit = - 1 ; while ( n > 0 ) { int curDigit = n % 10 ; if ( prevDigit != - 1 ) { if ( Math . abs ( curDigit - prevDigit ) != 1 ) return false ; } n /= 10 ; prevDigit = curDigit ; } return true ; } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }