Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Algorithm to convert decimal to octal number
3.
C++ program to convert a decimal to an octal number
3.1.
CODE
3.2.
OUTPUT
3.3.
EXPLANATION
3.4.
TIME AND SPACE COMPLEXITY
4.
Frequently Asked Questions
4.1.
How are octal and decimal numbers denoted?
4.2.
What do you mean by base value?
4.3.
What is the algorithm used in the above program?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

C++ Program to Convert Decimal Number to Octal

Author Ayushi Poddar
0 upvote

Introduction

In computer systems, octal numbers are denoted in the octal numeral system, while decimal numbers are in the decimal system. The decimal number is in base 10 using numbers 0 to 9, while the octal is in base 8 and uses 0 to 7. The base value of a number system tells us the number of digits used to represent a numeric value.

Some examples of decimal numbers to octal numbers.

Algorithm to convert decimal to octal number

  1. First, we divide the input, i.e., the decimal number, by 8 and then store the remainder.
  2. Then, we store the quotient back to the input number variable.
  3. We repeat this process till the quotient becomes zero.
  4. The equivalent octal number is the remainders in the above process in reverse order.

Also Read - C++ Interview Questions

C++ program to convert a decimal to an octal number

CODE

#include <iostream>
//header file for I/O
using namespace std;
void Decimal_To_Octal(int decimal_Num) 
{
   int octal_Num = 0, placeValue = 1;
   int dNo = decimal_Num;
   while (decimal_Num != 0) 
{
      octal_Num += (decimal_Num % 8) * placeValue;
      decimal_Num /= 8;
      placeValue *= 10;
   }
   cout<<"Octal form of the decimal number "<<dNo<<" is: "<<octal_Num<<endl;
}
int main() {
   Decimal_To_Octal(70);
   return 0;
}

OUTPUT

Octal form of the decimal number 70 is: 106

 

You can try by yourself with the help of online C++ Compiler.

EXPLANATION

In the above program, the function Decimal_To_Octal convert the decimal numbers into octal numbers. In the function Decimal_To_Octal, initially, the variable octal_Num is initialized to zero. The location of the digit of the number is denoted by the placeValue. The value of octal_Num is found using a while loop. For each iteration of the while loop, the decimal_Num is divided by 8, and the remainder is multiplied by the placeValue. This is then added to the previous value of octal_Num. Also, decimal_Num is divided by 8, and then the quotient is stored back. The placeValue is multiplied by 10.

TIME AND SPACE COMPLEXITY

Time Complexity: O(log N)

Space Complexity: O(1) 

As we, Initialize octal_num to 0 and placeValue to 1 and the decimal number as decimal_num and then find the remainder when decimal number divided by 8 and update the octal number by octal_Num + ((decimal_Num % 8)* placeValue) and increase placeValue by placeValue*10 and divide the decimal number by 8 and repeat the process from the second step until the decimal number is zero.

Must read decimal to binary c++ and Application of Oops

Frequently Asked Questions

How are octal and decimal numbers denoted?

In computer systems, octal numbers are represented in the octal numeral system, while decimal numbers are in the decimal system. The decimal number is in base 10 using numbers 0 to 9, while the octal is in base 8 and uses 0 to 7. 

What do you mean by base value?

The base value of a number system tells us the number of digits used to represent a numeric value.

What is the algorithm used in the above program?

We initialize octal_num to 0 and placeValue to 1 and the decimal number as decimal_num and then find the remainder when the decimal number is divided by 8 and update the octal number by octal_Num + ((decimal_Num % 8)* placeValue) and increase placeValue by placeValue*10 and divide the decimal number by 8 and repeat the process from the second step until the decimal number is zero.

Conclusion

This article extensively discussed the C++ Program to Convert Decimal Numbers to Octal. We hope that this blog has helped you gain more knowledge regarding the conversion of decimal to octal numbers, check out our articles on Coding Ninjas and solve this question to practice.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingBinary to Decimal numbersSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass