FOR FREE CONTENT

Decimal to Hexadecimal

Back to Programming

Description

The program is written here to convert the decimal number to hexadecimal. The decimal number is taken as input. For conversion, the decimal number is divided by 16, then, the quotient after division is again divided by 16 and this process repeats until the number becomes 0.

For example let the input be 135.

Therefore, the equivalent hexadecimal number is (87)16

Algorithm

INPUT: A decimal number

OUTPUT: Equivalent Hexadecimal Number

PROCESS:

Step 1: [Taking the input]

               Read n [the decimal number]

Step 2: [Converting from decimal to hexadecimal]

               Set s<-null [A string to store the hexadecimal number]

While d>0 repeat

                        Set r<- d mod 16

                        If r≤9 then

                                    Concatenate the value of variable ‘r’ with ‘s’ in reversed order.

                        Else if r=10 then

                                    Concatenate ‘A’ with ‘s’ in reversed order.

                        Else if r=11 then

                                    Concatenate ‘B’ with ‘s’ in reversed order.

                        Else if r=12 then

                                    Concatenate ‘C’ with ‘s’ in reversed order.

                        Else if r=13 then

                                    Concatenate ‘D’ with ‘s’ in reversed order.

                        Else if r=14 then

                                    Concatenate ‘E’ with ‘s’ in reversed order.

                        Else if r=15 then

                                    Concatenate ‘F’ with ‘s’ in reversed order.

                        Set d<-d/16

            [End of ‘while’ loop]

            [Printing the hexadecimal number]

            Print "The hexadecimal number is: s”

Step 3: Stop.

Code

TIME CMPLEXITY:

while(n>0) --------------------------------O(log n)

               {

                              r=n%16;

                              if(r<=9)

                                             hex[p++]=(char)(r+48);

                              else if(r==10)

                                             hex[p++]='A';

                              else if(r==11)

                                             hex[p++]='B';

                              else if(r==12)

                                             hex[p++]='C';

                              else if(r==13)

                                             hex[p++]='D';

                              else if(r==14)

                                             hex[p++]='E';

                              else if(r==15)

                                             hex[p++]='F';

                              n=n/16;

               }

The time complexity of this program is O(log n) where ‘n’ is the given decimal number.

SPACE COMPLEXITY:

The space complexity of this program is O(1) as the program requires a constant memory spaces to execute the program.