Check if a number starts with another number or not | Java program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into Strings ; Find the lengths of Strings s1 and s2 ; Base Case ; Traverse the Strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Driver Code ; Given numbers ; Function call ; If B is a prefix of A , then print " Yes "
class GFG { static boolean checkprefix ( int A , int B ) { String s1 = String . valueOf ( A ) ; String s2 = String . valueOf ( B ) ; int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 < n2 ) { return false ; } for ( int i = 0 ; i < n2 ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( 1 ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int A = 12345 , B = 12 ; boolean result = checkprefix ( A , B ) ; if ( ! result ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }