十进制正整数转指定进制(二进制、八进制、十六进制)
程序员文章站
2022-07-15 09:28:28
...
#include<stdio.h>
#define N 100
void main(){
char b[]="0123456789ABCDEF";
int c[64],d,i=0,base;
long n;
printf("n:");
scanf("%ld",&n);
do{
printf("base:");
scanf("%d",&base);
}
while(base!=2&&base!=8&&base!=16);
do{
c[i]=n%base;
i++;
n=n/base;
}while(n!=0);
for(--i;i>=0;--i){
d=c[i];
printf("%c",b[d]);
}
printf("\n");
}