Home > Net >  Trouble with a decimal to hexadecimal function that doesn't return anything
Trouble with a decimal to hexadecimal function that doesn't return anything

Time:10-04

When I compile this nothing returns and it is just empty space where I can type, but nothing happens instead of computing the program, Any reasons why?

#include <stdio.h>
void decimalToHex(long int decValue);

int main()
{
    long int decValue = 20;
    decimalToHex(decValue);

    return 0;
}

void decimalToHex(long int decValue)
{
    long int remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];
    quotient = decValue;
    while(quotient!=0)
    {
        temp = quotient % 16;
        if( temp < 10)
        temp =temp   48;
        else
        {
            temp = temp   55;
            hexadecimalNumber[i  ]= temp;
            quotient = quotient / 16;
        }
    }
    printf("Equivalent hexadecimal value of decimal number %ld: ",decValue);
    for(j = i -1 ; j> 0; j--)
    printf("%c",hexadecimalNumber[j]);
    printf("\nGoodbye!\n");
}

CodePudding user response:

            hexadecimalNumber[i  ]= temp;
            quotient = quotient / 16;

Hmm shouldn't this be outside the else? Yes it should.

while(quotient!=0)

While we're at it, this should be a do/while loop not a while loop. Always go around the loop once.

  •  Tags:  
  • c
  • Related