- C Programming Examples
- C Programming Examples
- C Print Hello World
- C Take Input from User
- C Print Integer
- C Add two Numbers
- C Check Even or Odd
- C Check Prime or Not
- C Check Alphabet or Not
- C Check Vowel or Not
- C Check Leap Year or Not
- C Check Reverse equal Original
- C Add Subtract Multiply & Divide
- C Make Calculator
- C Add Digits of Number
- C Calculate Average & Percentage
- C Calculate Arithmetic Mean
- C Calculate Student Grade
- C Print Table of Number
- C Print Prime Numbers
- C Add n Numbers
- C Interchange Numbers
- C Reverse Numbers
- C Swap two Numbers
- C Count Positive Negative & Zero
- C Find Largest of two Numbers
- C Find Largest of three Numbers
- C Find Factorial of Number
- C Find HCF & LCM
- C Calculate Area & Perimeter
- C Calculate Area & Circumference
- C Convert Fahrenheit to Centigrade
- C Convert Centigrade to Fahrenheit
- C Print ASCII Values
- C Print Fibonacci Series
- C Check Palindrome or Not
- C Check Armstrong or Not
- C Generate Armstrong Numbers
- C Find ncR nPr
- C Convert Decimal to Binary
- C Convert Decimal to Octal
- C Convert Decimal to Hexadecimal
- C Convert Binary to Decimal
- C Convert Binary to Octal
- C Convert Binary to Hexadecimal
- C Convert Octal to Decimal
- C Convert Octal to Binary
- C Convert Octal to Hexadecimal
- C Convert Hexadecimal to Decimal
- C Convert Hexadecimal to Binary
- C Convert Hexadecimal to Octal
- C Pattern Printing Programs
- C Print Diamond Pattern
- C Print Floyd Triangle
- C Print Pascal Triangle
- C Print smiling face
- C One Dimensional Array Program
- C Linear Search
- C Binary Search
- C Add Two Numbers using Pointer
- C Find Largest Element in Array
- C Find Smallest Element in Array
- C Reverse Array
- C Insert Element in Array
- C Delete Element from Array
- C Merge two Array
- C Bubble Sort
- C Selection Sort
- C Insertion Sort
- C Two Dimensional Array Program
- C Add two Matrices
- C Subtract Matrices
- C Transpose Matrix
- C Multiply Matrices
- C Three Dimension Array Program
- C Print String
- C Find Length of String
- C Compare two String
- C Copy String
- C Concatenate String
- C Reverse String
- C Delete Vowels from String
- C Delete Words from Sentence
- C Find Frequency of Character
- C Count Word in Sentence
- C Remove Spaces from String
- C Sort a String
- C Convert Uppercase to Lowercase
- C Convert Lowercase to Uppercase
- C Swap two Strings
- C Check Anagram or Not
- C Generate Random Numbers
- C Read File
- C Write to File
- C Read & Display File
- C Copy File
- C Merge two File
- C List Files in Directory
- C Delete File
- C Encrypt and Decrypt Files
- C Print Date
- C Get IP Address
- C Shutdown Computer
- C New Programs
- C Compute Charge to Send Parcel
- C Compute Telephone Bills
- C Find Discounted Purchase Amount
- C Find Wage of Labour
- C Round Off Floating-point Number
- C Find Bonus & Gross Salary
- C Sum/Product of Even/Odd Digit
- C Print Series upto N Term
- C Find Sum of All Digit
- C Find Product of All Digit
- C Find Total Number of Digit
- C Print all Factors of Number
- C Check Perfect Number or Not
- C Find Second Largest & Smallest
- C Print Sum of Row & Column
- C Common Element in Two Array
- C Count Space Tab Newline in file
- C Count with/without Space
- C Smallest of Three Numbers
- C Smallest of Two Numbers
- C Remove extra Spaces from String
- C Programming Tutorial
- C Tutorial
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- Register Now
- Login
- All Test List

In this tutorial, we will learn about how to create a program in C that converts any given decimal number (by user at run-time) into its equivalent hexadecimal value. We have also created a user-defined function-driven program that does the same job of converting decimal number to hexadecimal number.

Decimal number has its base as **10**. That means it has total of 10 digits that are **0, 1, 2, ..., 8, 9**. And Hexadecimal
number has its base as **16**. That means it has total of 16 digits that are **0, 1, 2, ..., 7, 8, 9, 10 (represented by A),
11 (represented by B), 12 (represented by C), 13 (represented by D), 14 (represented by E), 15 (represented by F)**. Because 10 has
two digit that are 0 and 1, in same way 11 has two digit that are 1 and 1 and so on. Therefore 10 is repsented with A, in same way 11
is represented with B, ..., 15 is represented with F.

To convert decimal number to hexadecimal number in C programming, you have to ask from user to enter the decimal number to convert it into hexadecimal number, and then display the equivalent value in hexadecimal as output.

Following C program asks from user to enter any number (in decimal) to convert it into hexadecimal, then display the result on the screen:

// Write a program in C that converts Decimal to Hexadecimal // --------codescracker.com-------- #include<stdio.h> #include<conio.h> int main() { int decnum, rem, i=0; char hexnum[50]; printf("Enter any decimal number: "); scanf("%d", &decnum); while(decnum!=0) { rem = decnum%16; if(rem<10) rem = rem+48; else rem = rem+55; hexnum[i] = rem; i++; decnum = decnum/16; } printf("\nEquivalent Value in Hexadecimal = "); for(i=i-1; i>=0; i--) printf("%c", hexnum[i]); getch(); return 0; }

As the above program was build and run under **Code::Blocks** IDE, therefore after successful build and run, you will get the
following output. This is the first snapshot of the sample run:

Now supply any decimal number as input say **172** and press ENTER key to see the equivalent hexadecimal value as output.
Here is the second snapshot of the sample run:

Here is another sample run. This is the final snapshot of the sample run:

Here are some of the main steps used in above program:

- Receive any decimal number by user
- Create a
**while**loop with condition**decnum!=0**. Here**decnum**variable holds the decimal number value entered by user at run-time - Let's suppose that user has entered
**172**as input - Therefore at first run of the
**while**loop, the condition**decnum!=0**or**172!=0**evaluates to be true. Therefore program flow goes inside the loop - And
**decnum%16**or**172%16**(we will get a remainder as 12 because while dividing the number 172 (dividend) with 16 (divisor), we will get 10 as quotient and 12 as remainder) or**12**gets initialized to**rem**variable - Now using
**if**statement check whether the value inside**rem**is less than 10 or not, because if value is less than 10, then the value may be from**0, 1, 2, ..., 8, 9**. And we all knows that the ASCII code for**0, 1, 2, ..., 8, 9**are**48, 49, 50, ..., 56, 57**. Therefore we have to apply the addition with 48 to the value present inside**rem**variable - Because if
**rem**is 0, then**rem+48**or**0+48**or**48**gets initialized to**rem**(which is the ASCII code for 0). Or if**rem**holds the value**4**, then**rem+48**or**4+48**or**52**gets initialized to**rem**that equals the ASCII code for**4** - And finally
**rem**(ASCII code value) gets initialized to**hexnum[i]**(**i**holds 0 as its initial value) or**hexnum[0]** - The second case is, if value of
**rem**is not less than 10, therefore value inside the**rem**variable may be from**10, 11, 12, ..., 14, 15** - In hexadecimal number system, 10 is repsented by A, 11 is repsented by B, ..., 15 is represented by F. And ASCII code for
**A, B, C, ..., E, F**are**65, 66, 67, ..., 69, 70** - Therefore,
**rem+55**gets initialized to**rem**. We have added the value of**rem**with**55**as if value of**rem**is 10, then we have to initialize ASCII code of A, as 10 is repsented by A. And the ASCII code of A is 65. In this way the ASCII code of**0, 1, 2, ..., E, or F**gets initialized to**hexnum[i]**or**hexnum[0]**. Therefore, at first run of the**while**loop. We have got a remainder value as**12**, therefore**rem+55**or**12+55**or**67**gets initialized to**rem**and then**rem**gets initialized to**hexnum[i]**or**hexnum[0]** - Then value of
**i**gets incremented and becomes 1. And**decnum/16**or**172/16**or**10**gets initialized to**decnum** - Now at second run of the
**while**loop, the condition**decnum!=0**or**10!=0**evaluates to be true, therefore program flow goes inside the loop again and do the similar operation as told in above steps - After exiting from the
**while**loop, we have successfully stored the equivalent hexadecimal value of given decimal number one by one - Therefore to print the number in hexadecimal form, we have to create a
**for**loop, that runs from**one less than the current value of i**to**0** - Because at last run, we have incremented the value of
**i**. Here last run means**decnum**contains 0, therefore the condition (decnum!=0 or 0!=0) evaluates to be false . Therefore we have to subtract the current value of**i**with 1 - Print all the digit of
**hexnum[]**array one by one from last digit to first digit - In this way, we have successfully printed equivalent hexadecimal value of given decimal value by user at run-time

Now let's create the same program but without using any modulous operator. That is we have to find out the remainder without using
modulous operator. Therefore to find out remainder, we have divided the number present inside **decnum** by 16, and stored its
quotient value inside a variable say **temp**, then again multiply the quotient value with 16 and stored the multiplication
result to a variable say **chck**. Now subtract **chck** from **decnum** (decnum - chck) and initialized it to
**rem** variable, that will holds the current remainder.

For example, if user has supplied 172 as input, therefore at first run **decnum/16** or **10** gets initialized to **temp**
Then **temp*16** or **10*16** or **160** gets initialized to **chck**. Now **decnum-chck** or **172-160** or
**12** gets initialized to **rem** which is the remainder this time. In this way, we have replaced the modulous operator with
three line of code as shown in the program given below:

// Write a program in C that converts any decimal number // (entered by user at run-time) to hexadecimal number // ---------codescracker.com--------- #include<stdio.h> #include<conio.h> int main() { int decnum, hexnum[50], temp, chck, i=0, rem; printf("Enter any Decimal number: "); scanf("%d", &decnum); while(decnum!=0) { temp = decnum/16; chck = temp*16; rem = decnum - chck; if(rem<10) rem = rem+48; else rem = rem+55; hexnum[i] = rem; i++; decnum = temp; } printf("\nEquivalent Hexadecimal Value = "); for(i=i-1; i>=0; i--) printf("%c", hexnum[i]); getch(); return 0; }

Here is the final snapshot of the sample run:

Now let's create a user-defined function named **DecToHex()** that takes one argument (the decimal number entered by user).
Here we have declared the variable **i** and the array **hex[]** outside both the function say **main()** and **DecToHex()**
to make it known for both the function. Here the variable **i** is declared as static variable because static variable remembers
its previous value. One last thing is, we have not initialized the static variable **i** with 0 (which is required), because
by default static variable holds 0 as its initial value after declaring the static variable. Rest of the thing is similar that can
easily be understand by you.

// Write a program in C that converts Decimal to Hexadecimal number // using user-defined function // --------codescracker.com-------- #include<stdio.h> #include<conio.h> void DecToHex(int dec); static int i; char hex[50]; int main() { int decnum; printf("Enter any decimal number: "); scanf("%d", &decnum); DecToHex(decnum); printf("\nEquivalent Value in Hexadecimal = "); for(i=i-1; i>=0; i--) printf("%c", hex[i]); getch(); return 0; } void DecToHex(int dec) { int rem; while(dec!=0) { rem = dec%16; if(rem<10) rem = rem+48; else rem = rem+55; hex[i] = rem; i++; dec = dec/16; } }

Below is the final snapshot of the sample run of above program:

You may also like same program in other programming languages:

- C++ Decimal to Hexadecimal Conversion
- Java Decimal to Hexadecimal Conversion
- Python Decimal to Hexadecimal Conversion