ahmed0saber

Decimal to Hex in C++

Nov 5th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. int main()
  5. {
  6.     int dec_num , r;
  7.     string hexdec_num="";
  8.     char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  9.     cout << "Input a decimal number : ";
  10.     cin>> dec_num;
  11.     if(dec_num>=0&&dec_num<=15)
  12.     {
  13.         while(dec_num>0)
  14.         {
  15.             r = dec_num % 16;
  16.             hexdec_num = hex[r] + hexdec_num;
  17.             dec_num = dec_num/16;
  18.         }
  19.         cout<<"The hexadecimal number is : "<<hexdec_num<<"\n";
  20.     }
  21.     else
  22.     {
  23.         cout<<dec_num<<" is an invalid input";
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment