C Program to Convert Hexadecimal to Octal

Write C Program to Convert Hexadecimal to Octal

// Write C Program to Convert Hexadecimal to Octal

#include <stdio.h>
#include <math.h>

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

	printf("Enter the Hexadecimal number :--> ");
	scanf("%d", &n);

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

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

	return 0;
}

Output:

Enter the Hexadecimal number :--> 654

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

≪ Previous | Next ≫