Given a number as string and a base, check if given number is in given base or not.
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 given base range. If yes, return true, else return false.
# 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):
# 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 [tabby title="C#"]
- Check whether a number has consecutive 0's in the given base or not
- Program to check if a number belongs to a particular base or not
- Check if a number is power of k using base changing method
- Complement of a number with any base b
- Convert a number into negative base representation
- Number System and base conversions | Digital Electronics
- Number of common base strings for two strings
- Double Base Palindrome
- Convert from any base to decimal and vice versa
- Convert all substrings of length 'k' from base 'b' to decimal
- Check Whether a number is Duck Number or not
- Check whether a given number is even or odd
- To check divisibility of any large number by 999
- Check whether given floating point number is even or odd
- Check if a large number is divisible by 11 or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.