C++ Program to Convert Decimal to Hexadecimal

Write C++ Program to Convert Decimal to Hexadecimal

// CPP Program to Convert Decimal to Hexadecimal

#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 % 16;  //here we take the last digit
        ans = (digit * pow(10,i)) + ans;	  //formula for the conversion
        n = n / 16;			// reducing the number
		i++;
	}
	cout << "The Hexadecimal number is :--> " << ans;

    return 0;
}

Output:

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

≪ Previous | Next ≫