Convert Decimal to Octal
Decimal to Octal in C++ (4 Programs)
IntermediateTopic: Advanced Number Programs
C++ Convert Decimal to Octal Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int decimal;
long long octal = 0;
int remainder, i = 1;
cout << "Enter a decimal number: ";
cin >> decimal;
int temp = decimal;
while (temp != 0) {
remainder = temp % 8;
temp /= 8;
octal += remainder * i;
i *= 10;
}
cout << "Decimal: " << decimal << " = Octal: " << octal << endl;
return 0;
}Output
Enter a decimal number: 10 Decimal: 10 = Octal: 12
Understanding Convert Decimal to Octal
This program converts decimal to octal by repeatedly dividing by 8 and collecting remainders. It demonstrates 4 different methods: using while loop, using recursion, using stringstream with oct manipulator, and using functions.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.