Decimal to Octal Conversion

Easy
0/40
Average time to solve is 15m
profile
Contributed by
1 upvote
Asked in companies
QualysMedTourEasySociete Generale

Problem statement

You are given a decimal number as input. You need to convert this number into its equivalent in the octal number system. The octal number system is the number system with a base value = 8.

A number system with base value = n means that all numbers, when written in this number system, will be represented with only digits from 0 to n-1. For example, the Binary number system has a base value = 2, so any number, when written in the binary system, will be represented using the digits 0 and 1 only.

Note:
The binary number system requires 2 digits (0-1), the Ternary number system requires 3 digits (0-2), the Octal number system requires 8 digits (0-7), and the decimal number system requires 10 digits (0-9) to represent any numeric value.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer 'T' representing the number of test cases. 

The first and the only line of each test case will contain an integer 'X', denoting the decimal number to be converted to the octal format.
Output Format:
For each test case, print a single line containing an integer denoting the octal value of 'X'.

The output of each test case will be printed in a separate line.
Note:
You don't have to print anything. it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
0 <= X <= 10 ^ 9

Time Limit: 1 sec.
Sample Input 1:
1
16
Sample Output 1:
20
Explanation for Sample Input 1:
Following is Octal Representation for first 16 decimal values.
Decimal       Octal
1         ->     1
2         ->     2
3         ->     3
4         ->     4
5         ->     5
6         ->     6
7         ->     7
8         ->     10   
9         ->     11
10        ->     12
11        ->     13
12        ->     14
13        ->     15
14        ->     16
15        ->     17
16        ->     20 
Sample Input 2:
1
8
Sample Output 2:
10 
Hint

Accumulate the remainders.

Approaches (1)
Maths
  • A necessary observation for octal numbers is that every digit will lie in the range 0 - 7.
  • Create a variable sol to store the final octal representation of X and initialise sol to 0.
  • So, to convert decimal numbers to octal numbers:

             1. Multiply previous sol value by 10, to add new remainder in the current step.

             2. Find the remainder when X is divided by 8 and add the remainder to sol.

             3. Update the value of X to X/8.

             4. Repeat the above three steps till X is not equal to 0.

 

  • Print the value of sol variable.
Time Complexity

O(log N), where ‘N’ is the given decimal value.

 

Each time we are dividing the number by 8. Therefore, if X is the number of times the loop runs, then, 8 ^ X ≥ N. Thus the overall time complexity becomes O(log N).

Space Complexity

O(1).

 

Since we are using constant extra memory.

Code Solution
(100% EXP penalty)
Decimal to Octal Conversion
Full screen
Console