Q:

Hexadecimal Number convertor

i trying to write a Recursion function that get a decimal number and print the value in hexadecimal 
but i dont know what worng in my code 
please help me i still new in Recursion and dont get it complitly...
    my code:

void PrintHexadecimalNumber(int n)
{
    if (n%16==0)
        return ;

    PrintHexadecimalNumber(n/16);

    switch ((n % 16))// A-F cases
    {
    case (10):
        printf("A");
        break;
    case (11):
        printf("B");
        break;
    case (12):
        printf("C");
        break;
    case (13):
        printf("D");
        break;
    case (14):
        printf("E");
        break;
    case (15):
        printf("F");
        break;
    }

    if ((n%16)<10)
        printf(n%16);

}

A:

Your code isn't going to work because of the test you've written to stop the recursion.
if (n%16==0)

If you have the number (in hex) 10, the function will stop immediately because n % 16 will be 0. What you actually want to test for is when n is 0. Like this
if (n==0)

Your switch statement should also be expanded out to include the values 0 to 9 rather than having a separate if statement after it. You can group them together by leaving out the break
switch(n % 16)
   {
/* existing case statements go here */
case 0:
case 1:
case 2:
...
case 9:
   printf("%d",n % 16); // also fixing this as per comments above.
break;
   }

Or you could just use printf("%x",n % 16) and do away with the switch completely if what you're trying to do is explore how recursion works.

