Saturday, April 20, 2013

c code to convert a number in base 10 to others(2-16)


//c code to convert a number in base 10 to others(2-16)
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main(){
clrscr();
int base,number,i=0,j;
static char digit[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int converted_num[8];
printf("enter a number to be converted and base (2 to 16)\n");
scanf("%d%d",&number,&base);
for(j=number;j>0;j/=base,i++){
converted_num[i]=j%base;
       // i++;
}
printf("%d in base %d is ",number,base);
for(--i;i>=0;--i){
j=converted_num[i];
printf("%c",digit[j]);
}
getch();
return 0;
}

No comments:

Post a Comment