There was an error while loading. Please reload this page.
1 parent 86cdfac commit 2d27344Copy full SHA for 2d27344
Any_Base_to_decimal
@@ -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