## Introduction to Decimal to Hexadecimal in C

Sometimes there is a mathematical calculation in programming for making that calculation happens we need to convert from decimal to hexadecimal which can be done in C easily in various ways. A decimal number includes numbers from 0 to 9 and the base number is 10 whereas for hexadecimal it includes numbers from 0 to 9 while including A, B, C, D, E, F and the base number is 16. Therefore, whenever a user is giving a decimal number as input we need to convert it into a hexadecimal number from base 10 to base 16.

### Syntax

Let’s have a look at the syntax and alongside we will see the steps for converting a number from decimal to hexadecimal :

**Step 1: **We have to divide the given number by 16 because we are converting the number into hexadecimal number.

**Step 2: **After that we have to again divide the remaining quotient by 16.

**Step 3: **We have to keep dividing the remaining quotient until our quotient turns to zero.

To explain the above step practically let’s take a number and convert it into the hexadecimal number.

#### Convert the number 800 in the hexadecimal

**Step 1:**Divide the number by 16. Therefore, 800 / 16 , Remainder : 0 , Quotient : 50

**Step 2: **Divide the quotient by 16. Therefore, 50 / 16, Remainder : 2 , Quotient : 3

**Step 3: **Divide the quotient by 16. Therefore, 3 / 16, Remainder : 3 , Quotient : 0

Final result, the converted decimal to hexadecimal number is: 320

( 800 ) 10 = ( 320 ) 16

As we have understood the process of converting the decimal number into hexadecimal mathematically, Now we will see programmatically to implement the algorithm to convert the number.

### Examples to Implement Decimal to Hexadecimal in C

Below are the examples mentioned:

#### Example #1

**Code:**

```
#include<stdio.h>
int main() {
long int num_decimal , remainder , quotient ;
int a = 1 , b , var ;
char hexanum_decimal[ 100 ] ;
printf( " Please enter decimal number here : " ) ;
scanf( "%ld" , &num_decimal ) ;
quotient = num_decimal ;
while( quotient != 0 ) {
var = quotient % 16 ;
if( var < 10 )
var = var + 48 ;
else
var = var + 55 ;
hexanum_decimal[ a++ ]= var ;
quotient = quotient / 16;
}
printf( " The equivalent hexadecimal value of decimal number is %ld : " , num_decimal ) ;
for ( b = a -1 ; b > 0 ; b-- )
printf( "%c" , hexanum_decimal[ b ] ) ;
return 0 ;
}
```

**Output :**

**Explanation:** As you can see in the above code, we are defining three long integer type variable named as “ num_decimal ”, “ remainder ”, “ quotient ”. Then we have declared integer variables a, b, and var where the value of integer variable a is assigned value equals 1. Then for hexadecimal number, we have to declare it as a character because it includes character values also. After that printf and scanf are used to take value from the user and display it on the screen.

Then the quotient value will be set equal to decimal number as of step 1 we have studied above. We are adding while the condition in which it will perform step 2 and step 3 until the quotient value becomes zero. If the condition is used to convert the given integer into the character. Finally, once quotient turns 0 it will print the hexadecimal value of the given decimal number.

#### Example #2

**Code:**

```
#include <stdio.h>
int main()
{
int num_decimal , temp , a ;
char hex [ 32 ] ;
printf( " Please enter the decimal number ( num_decimal ) here : " ) ;
scanf( "%d", &num_decimal ) ;
temp = 0 ;
while( num_decimal > 0 )
{
switch( num_decimal % 16 )
{
case 10 :
hex [ temp ] = 'A' ; break ;
case 11 :
hex [ temp ] = 'B'; break ;
case 12 :
hex [ temp ] = 'C' ; break ;
case 13 :
hex [ temp ] = 'D'; break ;
case 14 :
hex [ temp ] = 'E' ; break ;
case 15 :
hex [ temp ] = 'F'; break ;
default :
hex [ temp ] = ( num_decimal%16 ) + 0x30 ;
}
num_decimal = num_decimal / 16 ;
temp ++ ;
}
printf( " The Hexadecimal value of the decimal number is: " ) ;
for( a= ( temp-1 ) ; a >= 0 ; a--)
printf( "%c" , hex[ a ] ) ;
return 0;
}
```

**Output :**

**Explanation: **As you can see the above code will perform the same set of operations but in switch and break statement way. We are taking the same value as input which you can see in the output screen. we are defining three integer type variable named as “ num_decimal ”, “ temp ”, “ a ”. Then for hexadecimal number, we have to declare it as a character because it includes character values also from A to F. After that printf and scanf are used to take value from the user and display it on the screen. We are adding while the condition in which it will perform a switch statements according to the condition applied on switch whenever we get temp value. If the condition is used to convert the given integer into the character. After that we will convert the value into hexadecimal value.

### Conclusion

Conversion of decimal number into hexadecimal using the above steps saves a huge amount of time in programming because of quick and correct results in the smallest possible time. In the case of large decimal numbers, this logic is proved to be efficient in many norms in computer programming.

### Recommended Articles

This is a guide to Decimal to Hexadecimal in C. Here we discuss an introduction to Decimal to Hexadecimal in C, with appropriate syntax and respective examples. You can also go through our other related articles to learn more –

3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses