Yaşar.R Yaşar.R - 26 days ago 9
C Question

base conversion in c programming language

could anyone point out what is wrong with this piece of code?? I'm trying to write a simple(without using arrays and stuff like that) program that would convert the base 10 numbers to any other base. i'm a beginner, I've just started coding in C

PS: As you can see i haven't written anything that would inverse the results, and i didn't receive any outputs from the compiler. it stopped working.

main()
{
int a,b,c;
printf("Please enter a number in base 10: ");
scanf("%d",&a);
printf("\nPlease enter the base that you want the number to be converted to: ");
scanf("%d",&b);
do
{
c=a%b;
printf("%d",c);
a/=b;
}while(c!=0);
}

Answer

Change your while loop to a != 0. You want to loop until you have reduced the input number to zero. Your code is terminating when the first digit is 0.