In this article, we are going to learn how to convert Decimal to Octal in C++. Here we are going to learn about two number systems one is octal and another is Decimal. Octal numbers are based on 8 numbers and are represented in numbers from 0 to 7. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9.
C++ Program to convert Decimal to Octal
We will write the below program to convert Decimal to octal with any built-in function.
#include<iostream>
using namespace std;
int main()
{
long long decimalNum, tmpDecimalNo;
long long octalNum;
int i, rem, place = 1;
octalNum = 0;
cout << "Please enter the decimal number: ";
cin >> decimalNum;
tmpDecimalNo = decimalNum;
while(tmpDecimalNo > 0)
{
rem = tmpDecimalNo % 8;
octalNum = (rem * place) + octalNum;
tmpDecimalNo = tmpDecimalNo/8;
place *= 10;
}
cout << "\nThe Octal equivalent of Decimal number " << decimalNum << " is " << octalNum;
return 0;
}
Output
Please enter the decimal number: 25
The Octal equivalent of Decimal number 25 is 31