FOR FREE CONTENT

Decimal to Octal

Back to Programming

Description

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

For example let the input be 135.

Therefore, the equivalent octal number is (207)8

Algorithm

INPUT: A decimal number

OUTPUT: Equivalent Octal Number

PROCESS:

Step 1: [Taking the input]

               Read n [the decimal number]

Step 2: [Converting from decimal to octal]

               Set p<-1

               While n>0 repeat

                              Set oct<-oct+(n mod 8)×p

                              Set n<-n/8

                              Set p<-p×10

               [End of ‘while’ loop]

               [Printing the octal number]

               Print "The octal number is: oct”

Step 3: Stop.

Code

TIME COMPLEXITY:

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

               {

                              oct=oct+(n%8)*p;

                              n=n/8;

                              p=p*10;

               }

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 it requires a constant number of memory spaces to execute the program for any given input.