cbinarydecimalradix

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);
}

Solution

  • 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.