Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- char ConvertToChar (int num) {
- if (num >= 0 && num <= 9)
- return char(num + '0');
- else
- return char(num - 10 + 'A');
- }
- void ReverseString(char *str) {
- int sz = strlen(str);
- for (int i = 0; i < sz / 2; i++) {
- char cur_char = str[i];
- str[i] = str[sz - i - 1];
- str[sz - i - 1] = cur_char;
- }
- }
- char* FromDecToBase(int num, int base, char res[], int index) {
- // base case
- if (num <= 0) {
- res[index] = '\0';
- ReverseString(res);
- return res;
- }
- // recursive case
- res[index] = ConvertToChar(num % base);
- FromDecToBase(num / base, base, res, index + 1);
- }
- int main() {
- int num, base;
- scanf("%d%d", &num, &base);
- char res[1000];
- printf("%s", FromDecToBase(num, base, res, 0));
- return 0;
- }
Add Comment
Please, Sign In to add comment