Tuesday 19 February 2013

convert decimal to hexadecimal number(using arrays)

#include<iostream>
#include<math.h>
using namespace std;
int main()
{ int dec,i=0,store[20];
cout<<"decimal number please.....\n";
cin>>dec;
while(dec!=0)
{  store[i]=dec%16;   //remainder represents hexadecimal equivalent
   dec=dec/16;
   i++;
}
for(;i>=0;i--)
{if(store[i]>=10)
    cout<<(char)(store[i]+55);   //55 added  to give equivalent ASCII value
     else
    cout<<store[i];

}

return 0;
}

No comments:

Post a Comment