C++ Program to Convert Decimal to Octal

Write C++ Program to Convert Decimal to Octal

// CPP Program to Convert Decimal to Octal

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int n ;
	long ans = 0;

	cout << "Enter the Decimal number :--> ";
	cin >> n;

	int i = 0;
	while(n != 0)
	{
		int digit = n % 8;  //here we take the last digit

			ans = (digit * pow(10,i)) + ans;	  //formula for the conversion

			n = n / 8;	// reducing the number
		i++;
	}
	cout << "\nThe Octal Number is :--> " << ans;
    return 0;
}

Output:

Enter the Decimal number :--> 56
The Octal Number is :--> 70

≪ Previous | Next ≫