# Check if a number is in given base or not

Given a number as string and a base, check if given number is in 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to one by one check if all digits are in given base range. If yes, return true, else return false.

## C++

 `// CPP program to check if given  ` `// number is in given base or not. ` `#include ` `#include ` `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 code ` `int` `main() ` `{ ` `    ``string str = ``"AF87"``; ` `    ``if` `(isInGivenBase(str, 16)) ` `    ``cout << ``"Yes"``; ` `    ``else` `    ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// 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 Class ` `public` `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

 `# 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 code ` `Str` `=` `"AF87"` `if` `(isInGivenBase(``Str``, ``16``)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) ` `     `  `# This code is contributed by Mohit Kumar `

## C#

 `// 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 Code ` `public` `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

 ` 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 ` `?> `

Output:

```Yes
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.