Convert Decimal To Hexadecimal Number C Program

By | May 27, 2016

C Program To Convert Decimal To Hexadecimal Number

Learn How To Convert Decimal To Hexadecimal Number in C Programming Language. This C code for Decimal Number to Hexadecimal Conversion makes use of pow() function, While loop and For loop.

Decimal Number

A Decimal Number has a base 10 and includes digits the following digits: 0 1 2 3 4 5 6 7 8 9

Hexadecimal Number

A Hexadecimal Number has a base 16 and includes the following values: 0 1 2 3 4 5 6 7 8 9 A B C D E F where,

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.

Method 1: C Program To Convert Decimal To Hexadecimal Value using While Loop

#include<stdio.h>
#include<math.h>

int main()
{
      int decimal_number, remainder, hexadecimal_number = 0;
      int count = 0;
      printf("Enter a Decimal Number:\t");
      scanf("%d", &decimal_number);	
      while(decimal_number != 0)
      {
            remainder = decimal_number % 16;
            hexadecimal_number = hexadecimal_number + remainder * pow(10, count);
            decimal_number = decimal_number / 16;
            count++;
      }
      printf("\nHexadecimal Equivalent:\t%d\n", hexadecimal_number);
      return 0;
}

Method 2: C Program For Decimal Number To Hexadecimal Conversion using For Loop

#include<stdio.h>
#include<math.h>

int main()
{
      int decimal_number, remainder, hexadecimal_number = 0;
      int count;
      printf("Enter a Decimal Number:\t");
      scanf("%d", &decimal_number);	
      for(count = 0; decimal_number > 0; count++)
      {
            remainder = decimal_number % 16;
            hexadecimal_number = hexadecimal_number + remainder * pow(10, count);
            decimal_number = decimal_number / 16;
      }
      printf("\nHexadecimal Equivalent:\t%d\n", hexadecimal_number);
      return 0;
}

Method 3: C Program To Convert Decimal To Hexadecimal Number using Functions

#include<stdio.h>
#include<math.h>

int decimal_to_hexadecimal(int x)
{
      int hexadecimal_number, remainder, count = 0;
      for(count = 0; x > 0; count++)
      {
            remainder = x % 16;
            hexadecimal_number = hexadecimal_number + remainder * pow(10, count);
            x = x / 16;
      }
      return hexadecimal_number;
}

int main()
{
      int decimal_number, result;
      printf("Enter a Decimal Number:\t");
      scanf("%d", &decimal_number);		
      result = decimal_to_hexadecimal(decimal_number);	
      printf("\nHexadecimal Equivalent:\t%d\n", result);
      return 0;
}

Output

C Program To Convert Decimal To Hexadecimal Number using While and For Loop

If you have any compilation error or doubts in this C Program For Decimal Value to Hexadecimal Number Conversion, let us know about it in the Comment Section below.

Recommended Programs
C Program For Converting Hexadecimal Number To Binary
C Program To Convert Uppercase Characters into Lowercase
C Program For Converting Hexadecimal Number To Decimal
C Program For Numbers to Words Conversion
C Program For Converting Decimal Number To Octal
C Program For Converting Octal Number To Decimal
C Program For Converting Decimal Number To Binary
C Program For Converting Binary Number To Decimal
C Program For Decimal into Binary, Hexadecimal and Octal Value Conversion

3 thoughts on “Convert Decimal To Hexadecimal Number C Program

  1. Bob

    Can you explain ” hexadecimal_number = hexadecimal_number + remainder * pow(10, count);” in the 3rd example please?

    Reply
  2. Tarang

    I tried executing the above program on an online IDE and it returns 11 as the hexadecimal value for both – 11 and 17. Note: Both 11 and 17 are decimal input values.

    Reply
  3. rahul

    this is not for proper conversion 45 decimal and 51 decimal gives the same values of hex according to this programm 33.

    Reply

Let's Discuss