Thursday, April 15, 2010

C++ exercises: hex and decimal


Q: Write a program that declares three integer variables i, j, k. Input a value for i in decimal and values for j and k in hex. Print all three variables in both hex and decimal.

#include <iostream>
 
using namespace std;
 
int main()
{
   int i, j, k;
 
   cout << "input i(dec), j(hex), k(hex):";
   cin >> i >> hex >> j >> k;
 
   cout << "i: " << hex << i << "(hex) " << dec << i << "(dec) " << endl;
   cout << "j: " << hex << j << "(hex) " << dec << j << "(dec) " << endl;
   cout << "k: " << hex << k << "(hex) " << dec << k << "(dec) " << endl;

   return 0;
}

No comments:

 
Get This <