C Program to Convert Decimal Number to Hexadecimal Number


C Program to Convert Decimal number into Hexadecimal :

Output :

Logic of This Program :

In this program firstly we need to accept decimal number from the user using following statement -

Now after accepting the number we are calling the function which can evaluate equivalent hexadecimal number.

Inside the function we have declared 3 variables. Below table will explain the significance of each and every variable used inside the function.

VariableSignificance of the Variable
numThis variable is used to store the actual decimal number. We are dividing this variable for finding the remainder and quotient.
rem[50]It is remainder array which is used to store the reminder when we divide the number by 16.
iUsed as subscript variable for remainder array i.e rem
lengthIt is used to keep track of the size of a reminder array.

Now perform below steps until the number becomes less than 0 -

  1. Divide the number with 16 and store reminder in an array
  2. Divide the number with 16 and store quotient in num variable
  3. Increment i and length

After coming out of the loop. We need to print reminder in reverse fashion. Now consider the following table in order to print array -

RemainderDisplay this
10A
11B
12C
13D
14E
15F

In order to print reminders greater than 10 , refer above table. This can be done with switch case.

Download Program :

[box]Download Code [/box]