-1

i was just writing a program to change a decimal number to another base(2<=radix<=16).
after running the program and printing the correct answer, I encountered an error that reads : "program has stopped working" . could you please take a look at my code and find where it dangles???!!! i am really confused.
this is my code:

int decimal, radix, pow = 1, temp;
printf("enter your number and the 2 <= radix <= 16\n");
scanf("%d%d",&decimal, &radix);
temp = decimal;
for(; temp >= radix; temp /= radix)// finding the greatest exponent of radix in decimal
    pow *= radix;
while(pow >= 1){
    decimal -= temp * pow;
    if(temp == 10)
        printf("A");
    else if(temp == 11)
        printf("B");
    else if(temp == 12)
        printf("C");
    else if(temp == 13)
        printf("D");
    else if(temp == 14)
        printf("E");
    else if(temp == 15)
        printf("F");
    else
        printf("%d",temp);
    pow /= radix;
    temp = decimal / pow;
}
puts("");  

i think the problem is because of "temp = decimal / pow" but how can I fix it??

AlirezaAsadi
  • 793
  • 2
  • 6
  • 21

1 Answers1

2

Check if pow is 0 when you are calculating temp = decimal / pow; at the end of while loop

    pow /= radix;
    if (pow > 0)
    {
        temp = decimal / pow;
    }
Gor Asatryan
  • 904
  • 7
  • 24
  • Oh I see. Thank you very much . Yeah division by zero also stroke my mind but I couldn't find where it equals to zero. Afterall , it is very hard to debug your own program. – AlirezaAsadi Nov 18 '17 at 12:43