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 Algorithms, Competitive Programming, Binary to Decimal numbers, System 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 problems, interview 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!