/*
Program for coverting "Decimal number into HexaDecimal".
*/
#include<iostream>
using namespace std;
int main()
{
long num, temp, i = 0, HD[10];
cout << "Enter Number?\t";
cin >> num;
temp = num;
//for calculating no. of digits in Hexa-Decimal
while (num!=0)
{
HD[i] = num % 16;
num /= 16;
i++;
}
cout << "HexaDecimal of " << temp << " is:\n";
for (int a = i - 1; a >= 0; a--)
{
if (HD[a] == 10)
cout << "A";
else if (HD[a] == 11)
cout << "B";
else if (HD[a] == 12)
cout << "C";
else if (HD[a] == 13)
cout << "D";
else if (HD[a] == 14)
cout << "E";
else if (HD[a] == 15)
cout << "F";
else
cout << HD[a];
}
cout << endl;
return 0;
}
OUTPUT
/*
Program for coverting "Decimal number into HexaDecimal" (Using 'iomanip').
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char response;
do
{
cout << "Enter positive integer to convert to Hexadecimal:\n\n";
int number;
cin >> number;
cout << "Hexadecimal representation of " << number << " is "
<< hex << uppercase << number << dec << '\n'
<< "\nDo u want to enter another number (Y or N)? ";
cin >> response;
} while (response == 'Y' || response == 'y');
return 0;
}
No comments:
Post a Comment