FOR FREE CONTENT

Octal to Decimal

Back to Programming

Description

The program is written here to convert an octal number into its equivalent decimal0 number. The octal number is taken as input then it is converted into its equivalent decimal number.

 For example, if an octal number is (75)8

Now, the equivalent decimal number will be calculated by multiplying each digit of the octal number with the increasing power of 8 (the power will starts from 0 and will increase from right to left).

Therefore, the equivalent decimal number:

81 + 5×80

= 56 + 5

=(61)10

Algorithm

INPUT: An octal number

OUTPUT: Equivalent decimal number

PROCESS:

Step 1: [Taking the input]

               Read n [an octal number]

Step 2: [Converting from octal to decimal]

               Set d<-0

               Set p<-1

                While n>0 repeat

                              [Calculating the power of 8 and multiplied with the octal digit]

                              Set d<-d+(n mod 10)×p

                              [Increasing the power]

                              Set p<-p×8

                              Set n<-n/10

               [End of ‘while’ loop]

               Print the decimal number stored in ‘d’

Step 3: Stop.

Code

TIME COMPLEXITY:

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

               {

                              //calculating the power of 8 and multiplied

                              //with the octal digit

                              d=d+(n%10)*p;

                              //increasing the power

                              p=p*8;

                              n=n/10;

               }

The time complexity of this program is O(m) where ‘m’ is the number of digits of the octal number.

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of memory spaces to execute for any given input.