This is a C++ Program to Convert a Decimal Number to Octal Equivalent.
The program takes a decimal number and converts it into its octal equivalent.
1. A decimal number is entered.
2. The number is divided by 8 and the remainders are stored.
3. The result is printed in reverse order.
4. Exit.
Here is the source code of C++ Program to Convert a Decimal Number to Octal Equivalent. The program output is shown below.
#include <iostream>
using namespace std;
int main()
{
long num, temp;
int oct[50], i = 1, j;
cout << "Enter a decimal number : ";
cin >> num;
temp = num;
while (temp != 0)
{
oct[i++] = temp % 8;
temp = temp / 8;
}
cout << "\nOctal equivalent of " << num << " is : ";
for (j = i-1; j >= 0; j--)
cout << oct[j];
return 0;
}
1. The user is asked to enter a decimal number.
2. Its value is stored in the long variable ‘num’ and copied in a temporary variable ‘temp’.
3. Using a while loop, the remainders of temp are calculated, dividing it by 8.
4. The remainders are stored in the array ‘oct’.
5. The loop terminates when quotient becomes 0.
6. The array oct is then printed in reverse order which is the octal equivalent of the number entered.
Case 1 : Enter a decimal number : 8 Octal equivalent of 8 is : 10 Case 2 : Enter a decimal number : 33 Octal equivalent of 33 is : 41 Case 3 : Enter a decimal number : 441 Octal equivalent of 441 is : 671
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.
- Check Computer Science Books
- Apply for Computer Science Internship
- Apply for C++ Internship
- Practice Programming MCQs
- Practice Computer Science MCQs