Print all numbers from a given range that are made up of consecutive digits | Java program for the above approach ; Function to find the consecutive digit numbers in the given range ; Initialize num as empty String ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Driver Code ; Print the required numbers
import java . util . * ; class GFG { static Vector < Integer > solve ( int start , int end ) { String num = " " ; Vector < Integer > ans = new Vector < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { num = Integer . toString ( i ) ; int value = i ; if ( value >= start && value <= end ) { ans . add ( value ) ; } for ( int j = i + 1 ; j <= 9 ; j ++ ) { num += Integer . toString ( j ) ; value = Integer . valueOf ( num ) ; if ( value >= start && value <= end ) { ans . add ( value ) ; } } } Collections . sort ( ans ) ; ; return ans ; } public static void main ( String [ ] args ) { int L = 12 , R = 87 ; Vector < Integer > ans = solve ( L , R ) ; for ( int it : ans ) System . out . print ( it + " ▁ " ) ; } }