Saturday, June 18, 2016

C Program Code: Convert Decimal to Hexadecimal

C Program Code:

#include<stdio.h>
void decimalToHex(int number , int base);
int main()
{
    int decimalNumber;
    int base;
    base = 16; // Base of Hexadecimal
    printf("Decimal to Hexadecimal Number Converter in C \n");
    printf("Enter number in decimal: ");
    scanf("%d",&decimalNumber);
    printf("Decimal %d = ",decimalNumber);
    decimalToHex(decimalNumber, base);
    printf(" Hex \n");
    printf("Press enter to continue... \n");
    fflush(stdin);
    getchar();
    return 0;
}
void decimalToHex(int number , int base)
{
    if( number > 0 )
    {
        decimalToHex( number/base , base );
        switch(number%base)
        {
        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;
        default :
            printf("%d",number%base);
            break;
        }
    }
}

Convert Decimal to Hexadecimal in C Output Screenshot