C++ Program to Convert Hexadecimal to Octal

C++ Program to Convert Hexadecimal to Octal

Write C++ Program to Convert Hexadecimal to Octal

// CPP Program to Convert Hexadecimal to Octal

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

using namespace std;

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

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

	int i = 0;
	while(n != 0)
	{
		int digit = n % 10;
		ans = ans + (digit * pow(16, i));
		n = n / 10;
		i++;
	}
	cout << "\nThe Decimal Number is :--> " << ans;

	int j = 0, ans2 = 0;
	while(ans != 0)
	{
		int digit = ans%8;
		ans2 = ans2 + (digit * pow(10, j));
		ans = ans / 8;
		j++;
	}
	cout << "\nThe Octal Number is :--> " << ans2;

	return 0;
}

Output:

Enter the Hexadecimal number :--> 654

The Decimal Number is :--> 1620
The Octal Number is :--> 3124