C Program to Convert Decimal to Hexadecimal

C Program to Convert Decimal to Hexadecimal

Write C Program to Convert Decimal to Hexadecimal

// Write C Program to Convert Decimal to Hexadecimal

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

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

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

	int i = 0;
	while(n != 0)
	{
		int digit = n % 16;  //here we take the last digit
        ans = (digit * pow(10,i)) + ans;	  //formula for the conversion
        n = n / 16;			// reducing the number
		i++;
	}
	printf("The Hexadecimal number is :--> %d",ans);

    return 0;
}

Output:

Enter the Decimal number :--> 564
The Hexadecimal number is :--> 234