-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAny_Base_to_decimal
More file actions
40 lines (34 loc) · 798 Bytes
/
Any_Base_to_decimal
File metadata and controls
40 lines (34 loc) · 798 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int base, i, j;
char number[100];
unsigned long decimal = 0;
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the number: ");
scanf("%s", &number[0]);
for (i = 0; number[i] != '\0'; i++)
{
if (isdigit(number[i]))
number[i] -= '0';
else if (isupper(number[i]))
number[i] -= 'A' - 10;
else if (islower(number[i]))
number[i] -= 'a' - 10;
else
number[i] = base + 1;
if (number[i] >= base)
{
printf("invalid number\n");
return 0;
}
}
for (j = 0; j < i; j++)
{
decimal *= base;
decimal += number[j];
}
printf("%lu\n", decimal);
}