C Program to check if a number belongs to a particular base or not

Given a number as a string and a base, the task is to check whether the given number belongs to that given base or not.

We have to check the number and the base according to the number system in which there are bases like 2 for a binary number, 8 for an octal number, 10 for decimal number and 16 for a Hexadecimal number. According to this we have to find whether the given number in a string belongs to a particular base or not. If it belongs to a particular base then we have to print "Yes" on the output screen; else "No" on an output screen.

Like we know that, the number/expression "1A6" is of the base 16 and "1010" is of base 2, but this can be judged by just visual analysis. Now we have to find a way to solve the problem with help of a program.

Syntax

bool isInGivenBase(char str[], int base);

Example Input/Output

Input: str = "1010", base = 2
Output: yes

Input: str = "1AA4", base = 16  
Output: yes

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

Approach

  • Check if the base lies between 2 to 16.
  • Then check each digit of the string belongs to the particular base or not.
  • If it belongs then return true, else false.

Example

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isInGivenBase(char 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.
    if (base <= 10) {
        for (int i = 0; i < strlen(str); i++)
            if (!(str[i] >= '0' &&
                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 < strlen(str); i++)
            if (!((str[i] >= '0' &&
                str[i] < ('0' + base)) ||
                (str[i] >= 'A' &&
                str[i] < ('A' + base - 10))))
                return false;
    }
    return true;
}

int main() {
    char str1[] = "AF87";
    char str2[] = "1010";
    char str3[] = "1610";
    
    printf("Testing '%s' in base 16: ", str1);
    if (isInGivenBase(str1, 16))
        printf("yes<br>");
    else
        printf("No<br>");
    
    printf("Testing '%s' in base 2: ", str2);
    if (isInGivenBase(str2, 2))
        printf("yes<br>");
    else
        printf("No<br>");
    
    printf("Testing '%s' in base 2: ", str3);
    if (isInGivenBase(str3, 2))
        printf("yes<br>");
    else
        printf("No<br>");
    
    return 0;
}
Testing 'AF87' in base 16: yes
Testing '1010' in base 2: yes
Testing '1610' in base 2: No

Key Points

  • For bases 2−10, only digits '0' to ('0' + base−1) are valid.
  • For bases 11−16, digits '0'−'9' and letters 'A' to ('A' + base−11) are valid.
  • The function returns true if all characters in the string are valid for the given base.

Conclusion

This program efficiently validates whether a given string represents a valid number in a specified base by checking each character against the allowed character set for that base. It handles both numeric bases (2−10) and alphanumeric bases (11−16).

Updated on: 2026-03-15T12:24:21+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements