Skip to content

Commit 2d27344

Browse files
authored
init decimal file
1 parent 86cdfac commit 2d27344

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Any_Base_to_decimal

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <ctype.h>
2+
#include <stdio.h>
3+
4+
int main(void)
5+
{
6+
int base, i, j;
7+
char number[100];
8+
unsigned long decimal = 0;
9+
10+
printf("Enter the base: ");
11+
scanf("%d", &base);
12+
printf("Enter the number: ");
13+
scanf("%s", &number[0]);
14+
15+
for (i = 0; number[i] != '\0'; i++)
16+
{
17+
if (isdigit(number[i]))
18+
number[i] -= '0';
19+
else if (isupper(number[i]))
20+
number[i] -= 'A' - 10;
21+
else if (islower(number[i]))
22+
number[i] -= 'a' - 10;
23+
else
24+
number[i] = base + 1;
25+
26+
if (number[i] >= base)
27+
{
28+
printf("invalid number\n");
29+
return 0;
30+
}
31+
}
32+
33+
for (j = 0; j < i; j++)
34+
{
35+
decimal *= base;
36+
decimal += number[j];
37+
}
38+
39+
printf("%lu\n", decimal);
40+
}

0 commit comments

Comments
 (0)