C program to Convert Decimal to Hexadecimal

Decimal to hexadecimal conversion in C:

We can convert any decimal number (base-10 (0 to 9)) into hexadecinal number(base-16 (0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15)) by c program.

Decimal Number

Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal number such as 23, 445, 132, 0, 2 etc.

Hexadecimal Number

Hexadecimal number is a base 16 number because it ranges from 0 or 9 and A(10),B(11),C(12),D(13),E(14),F(15). Any combination of digits is Hexadecimal number such as A34, 5A4, 7DE7, ABCD etc.

To understand this example, you should have the knowledge of the following C programming topics:

C Functions
C User-defined functions

/*
 * C program to Convert Decimal to Hexadecimal
 */
#include <stdio.h>

int main()
{
    long decimalnum, quotient, remainder;
    int i, j = 0;
    char hexadecimalnum[100];

    printf("Enter decimal number: ");
    scanf("%ld", &decimalnum);

    quotient = decimalnum;

    while (quotient != 0)
    {
        remainder = quotient % 16;
        if (remainder < 10)
            hexadecimalnum[j++] = 48 + remainder;
        else
            hexadecimalnum[j++] = 55 + remainder;
        quotient = quotient / 16;
    }

    // display integer into character
    for (i = j-1; i >= 0; i--)
            printf("%c", hexadecimalnum[i]);
    return 0;
}

Output:

Enter decimal number: 159
9F
Enter decimal number: 37
25

Programming Solutions @ rajeshshukla

All Programs
C Language Simple Programs
C Language If Condition and Switch Statement Programs
C Language Loop Programs
C Language Conversion Programs
C Language Pattern Programs
C Language Single Dimension Numeric Array programs
C Language Double Dimension Numeric Array Programs
C Language Single Dimension Character Array programs
C Language Pointer Programs
C Language File Handling Programs

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Class 11 (Python)
Class 12 (Python)
C Language
C++ Programming
Python

C Interview Questions
C++ Interview Questions


Number Of Visitors

0125594