Check if a number is in given base or not | C # program to check if given number is in given base or not . ; Allowed base are till 16 ( Hexadecimal ) ; If bas is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Code
using System ; class GFG { static bool isInGivenBase ( String str , int bas ) { if ( bas > 16 ) return false ; else if ( bas <= 10 ) { for ( int i = 0 ; i < str . Length ; i ++ ) if ( ! ( str [ i ] >= '0' && str [ i ] < ( '0' + bas ) ) ) return false ; } else { for ( int i = 0 ; i < str . Length ; i ++ ) if ( ! ( ( str [ i ] >= '0' && str [ i ] < ( '0' + bas ) ) || ( str [ i ] >= ' A ' && str [ i ] < ( ' A ' + bas - 10 ) ) ) ) return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " AF87" ; if ( isInGivenBase ( str , 16 ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }