Created
March 5, 2018 18:22
-
-
Save gallirohik/d763e6195a03ecb6b3cab52f5fa19626 to your computer and use it in GitHub Desktop.
conver decimal to any number system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
char value(int val) | |
{ | |
if(val>=0&&val<=9) | |
return val+'0'; | |
return val-10+'A'; | |
} | |
void convert_to(int n,int base,char *p) | |
{ | |
if(n==0) | |
return ; | |
*p=(char)value(n%base); | |
convert_to((int)n/base,base,p+1); | |
printf("%c",*p); | |
*p='\0'; | |
} | |
void r_r_string(char *str) | |
{ | |
if(*(str)=='\0') | |
return ; | |
r_r_string(str+1); | |
printf("%c",*str); | |
} | |
int main() | |
{ | |
int n,base; | |
char *result; | |
result=(char*)malloc(50*sizeof(char)); | |
scanf("%d",&n); | |
scanf("%d",&base); | |
convert_to(n,base,result); | |
printf("%s",result); | |
r_r_string(result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment