Sunday, August 23, 2015

Decimal to Hexadecimal Conversion

Q. 100. C++ program to convert a Decimal number to its Hexadecimal format.


Copy Code:

#include <iostream>
using namespace std;

int main()

{
  int i, n, m, a;
  char ch[20];
  cout << "Enter a Number : ";
  cin >> n;
  m = n;
  for ( i=0 ; n>0 ; ++i )
  {
     a = n%16;
     n = n/16;
     if (a<10)
     {
       ch[i] = a+48;
      }
      else
        ch[i] = a+55;
  }

  cout << "Hexadecimal Equivalent of " << m << " is : ";

  for ( i = i - 1 ; i >= 0 ; --i )
     cout << ch[i];
    
  return 0;
}

No comments:

Post a Comment