-
Notifications
You must be signed in to change notification settings - Fork 334
/
DecimalToBaseN.c
44 lines (35 loc) · 947 Bytes
/
DecimalToBaseN.c
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
41
42
43
44
// Convert base10(decimal) values any base
#include<stdio.h>
void printDigit(int, int);
int main(){
int input, base;
// Getting user input
printf("Enter number: ");
scanf("%d", &input);
printf("Enter base: ");
scanf("%d", &base);
printf("%d to base %d : ", input, base);
printDigit(input, base);
return 0;
}
// This is a reccursive function that prints digit by digit
void printDigit(int num, int base){
if(num > 0){
// getting current digit to be printed
int digit = num % base;
num = num - digit;
num = num / base;
// Call to function to print next digit
printDigit(num, base);
// If the current digit is more than 9 (eg: 10=A, 11=B)
if(digit > 9){
printf("%c", 55 + digit);
}
// Else print the digit directly
else {
printf("%d", digit);
}
} else {
return;
}
}