Problem 1: (Decimal to hex) Write a program that converts a nonnegative decimal
ID: 3831752 • Letter: P
Question
Problem 1: (Decimal to hex) Write a program that converts a nonnegative decimal number into a hexadecimal number. The input and output should be exactly Enter a decimal number: CUSER ENTERS A NONNEGATIVE INTEGERJ Your number in hex is OxXX. For example, an input of 4095 should produce the output Your number in hex is 0xFFF. You may not use any libraries aside from iostream and string You may not use the dec, hex, or oct format flags provided by the iostream library. You may not use the stoi, stol, stoul stoll, stoull, stof, stod, and stold functions provided by the string library. Hint. Consider the following code int i 15 char c static-cast Char (i 10 'A' string s string (1 c) convert char into a string of length 1 cout endl The output is F.Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
cout<<"Enter a decimal number: ";
cin>>decimalNumber;
quotient = decimalNumber;
while(quotient!=0)
{
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
cout<<"Your number in hex is 0x";
for(j = i -1 ;j> 0;j--)
cout<<hexadecimalNumber[j];
cout<<endl;
return 0;
}