C++ Program to Converter a Decimal to Octal

Last Updated:

Converter a Number from Decimal to Octal

In the following example, we will convert a Decimal Number (100) to Octal Number (144).

Tips: It is recommended to use our online Decimal to Octal calculator for better understanding.

Example

C++ Compiler
#include <iostream> #include <cmath> using namespace std; int main() { int num = 23456; int arr[50], answer[50]; int value = 1; int i, j = 0; int count = -1; while(num > value) { count++; value = (int) pow(8, count); arr[count] = value; } count = count - 1; for(i=count; i>=0; i--) { answer[j] = num / arr[i]; j++; num = num % arr[i]; } cout << "Octal number of 23456 (decimal) is "; for(i=0; i<=count; i++) cout << answer[i]; return 0; }

Converter any Given Decimal Number to Octal Number

In the following example, we will convert any given decimal number to an octal number.

Example

C++ Compiler
#include <iostream> #include <cmath> using namespace std; int main() { int num, copyNum, arr[50], answer[50]; int value = 1; int i, j = 0; int count = -1; cout << "Enter a Decimal Number: "; cin >> num; copyNum = num; while(copyNum > value) { count++; value = (int) pow(8, count); arr[count] = value; } count = count - 1; for(i=count; i>=0; i--) { answer[j] = copyNum / arr[i]; j++; copyNum = copyNum % arr[i]; } cout << "Octal number of " << num << " (decimal) is "; for(i=0; i<=count; i++) cout << answer[i]; return 0; }

Share this Page

Meet the Author