Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 23 Oct, 2020

Easy

```
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.
```

```
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.
```

```
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.
```

```
You don't have to print anything. it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 10
0 <= X <= 10 ^ 9
Time Limit: 1 sec.
```

- 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.

Similar problems

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Ninja And The Strictly Increasing Array

Moderate

Posted: 27 Nov, 2022

Maximum GCD

Hard

Posted: 8 Dec, 2022

Prime?

Easy

Posted: 9 Dec, 2022

Co-Prime

Hard

Posted: 14 Dec, 2022