Check if a number is in given base or not

22 Jun, 2022
Given a number as a string and a base, check if the given number is in the given base or not.Examples:  
Input : str = "1010", base = 2
Output : Yes

Input : str = "1015", base = 2
Output : No

Input : str = "AF87", base = 16
Output : Yes
 
The idea is to one by one check if all digits are in the given base range. If yes, return true, else return false.
C++
Java
Python3
C#
PHP
Javascript
// CPP program to check if given// number is in given base or not.#include <cstring>#include <iostream>using namespace std; bool isInGivenBase(string str, int base){    // Allowed bases are till 16 (Hexadecimal)    if (base > 16)    return false;     // If base is below or equal to 10, then all    // digits should be from 0 to 9.    else if (base <= 10)    {    for (int i = 0; i < str.length(); i++)        if (!(str[i] >= '0' and             str[i] < ('0' + base)))            return false;    }     // If base is below or equal to 16, then all    // digits should be from 0 to 9 or from 'A'    else    {    for (int i = 0; i < str.length(); i++)        if (! ((str[i] >= '0' &&                str[i] < ('0' + base)) ||                                                (str[i] >= 'A' &&                 str[i] < ('A' + base - 10))            ))                            return false;    }    return true;} // Driver codeint main(){    string str = "AF87";    if (isInGivenBase(str, 16))    cout << "Yes";    else    cout << "No";    return 0;}
// Java program to check if given// number is in given base or not.class Geeks {     static boolean isInGivenBase(String str, int base){         // Allowed bases are till 16 (Hexadecimal)    if (base > 16)    return false;     // If base is below or equal to 10, then all    // digits should be from 0 to 9.    else if (base <= 10)    {    for (int i = 0; i < str.length(); i++)        if (!(str.charAt(i) >= '0' &&              str.charAt(i) < ('0' + base)))            return false;    }     // If base is below or equal to 16, then all    // digits should be from 0 to 9 or from 'A'    else    {    for (int i = 0; i < str.length(); i++)        if (! ((str.charAt(i) >= '0' &&                str.charAt(i) < ('0' + base)) ||                                            (str.charAt(i) >= 'A' &&                 str.charAt(i) < ('A' + base - 10))            ))                        return false;    }    return true;} // Driver Classpublic static void main(String args[]) {    String str = "AF87";    if (isInGivenBase(str, 16) == true)    System.out.println("Yes");    else    System.out.println("No");     }} // This code is contributed by ankita_saini
# Python3 program to check if given# number is in given base or not. def isInGivenBase(Str, base):     # Allowed bases are till 16 (Hexadecimal)    if (base > 16):        return False     # If base is below or equal to 10,    # then all digits should be from 0 to 9.    elif (base <= 10):        for i in range(len(Str)):            if (Str[i].isnumeric() and               (ord(Str[i]) >= ord('0') and                ord(Str[i]) < (ord('0') + base)) == False):                return False         # If base is below or equal to 16, then all    # digits should be from 0 to 9 or from 'A'    else:        for i in range(len(Str)):            if (Str[i].isnumeric() and               ((ord(Str[i]) >= ord('0') and                 ord(Str[i]) < (ord('0') + base)) or                (ord(Str[i]) >= ord('A') and                 ord(Str[i]) < (ord('A') + base - 10))) == False):                return False         return True # Driver codeStr = "AF87"if (isInGivenBase(Str, 16)):    print("Yes")else:    print("No")     # This code is contributed by Mohit Kumar
// C# program to check if given// number is in given base or not.using System; class GFG{static bool isInGivenBase(String str,                          int bas){         // Allowed base are    // till 16 (Hexadecimal)    if (bas > 16)    return false;     // If bas is below or equal    // to 10, then all digits    // should be from 0 to 9.    else if (bas <= 10)    {    for (int i = 0; i < str.Length; i++)        if (!(str[i] >= '0' &&              str[i] < ('0' + bas)))            return false;    }     // If base is below or equal    // to 16, then all digits should    // be from 0 to 9 or from 'A'    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;} // Driver Codepublic static void Main(String []args){    String str = "AF87";    if (isInGivenBase(str, 16) == true)    Console.WriteLine("Yes");    else    Console.WriteLine("No");}} // This code is contributed// by ankita_saini
<?php// PHP program to check if given// number is in given base or not. function isInGivenBase($str, $base){    // Allowed bases are till    // 16 (Hexadecimal)    if ($base > 16)    return false;     // If base is below or equal to    // 10, then all digits should    // be from 0 to 9.    else if ($base <= 10)    {    for ($i = 0; $i < strlen($str); $i++)        if (!($str[$i] >= '0' and            $str[$i] < ('0' + $base)))            return false;    }     // If base is below or equal to 16,    // then all digits should be from    // 0 to 9 or from 'A'    else    {    for ($i = 0; $i < strlen($str); $i++)        if (! (($str[$i] >= '0' &&                $str[$i] < ('0' + $base)) ||                                           ($str[$i] >= 'A' &&                $str[$i] < ('A' + $base - 10))            ))                        return false;    }    return true;} // Driver code$str = "AF87"; if (isInGivenBase($str, 16))    echo "Yes";else    echo "No"; // This code is contributed by jit_t?>
<script>     // Javascript program to check if given    // number is in given base or not.         function isInGivenBase(str, bas)    {         // Allowed base are        // till 16 (Hexadecimal)        if (bas > 16)            return false;         // If bas is below or equal        // to 10, then all digits        // should be from 0 to 9.        else if (bas <= 10)        {          for (let i = 0; i < str.length; i++)              if (!(str[i].charCodeAt() >=              '0'.charCodeAt() &&               str[i].charCodeAt() <               ('0'.charCodeAt() + bas)))                  return false;        }         // If base is below or equal        // to 16, then all digits should        // be from 0 to 9 or from 'A'        else        {          for (let i = 0; i < str.length; i++)              if (! ((str[i].charCodeAt() >=              '0'.charCodeAt() &&               str[i].charCodeAt() <               ('0'.charCodeAt() + bas)) ||                                       (str[i].charCodeAt() >=               'A'.charCodeAt() &&                str[i].charCodeAt() <                ('A'.charCodeAt() + bas - 10))                  ))                          return false;        }        return true;    }         let str = "AF87";    if (isInGivenBase(str, 16) == true)        document.write("Yes");    else        document.write("No"); </script>
Yes
Time complexity: O(n), where n is the length of the given string.Auxiliar Space:O(1)
ankita_saini
jit_t
mohit kumar 29
divyesh072019
hasani
base-conversion
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different Methods to Reverse a String in C++
Python program to check if a string is palindrome or not
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Top 50 String Coding Problems for Interviews
Convert string to char array in C++
Check whether two strings are anagram of each other
Reverse words in a given string