Site icon DataFlair

C++ Program to Convert Decimal to Hexadecimal

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Decimal to Hexadecimal conversion
#include<iostream>
#define clrscr() system("cls")
using namespace std;

int main()
{
      int d,hexa[100],i,r;
      clrscr();
      cout<<"\nEnter decimal number";
      cin>>d;
      i=0;
      while(d!=0)
      {
          r=d%16;
          hexa[i]=r;
          d=d/16;
          i++;
      }
      
      i--;
      while(i>=0)
      {
           switch(hexa[i])
           {
            case 10:cout<<"A";break;
            case 11:cout<<"B";break;
            case 12:cout<<"C";break;
            case 13:cout<<"D";break;
            case 14:cout<<"E";break;
            case 15:cout<<"F";break;
            default:cout<<hexa[i];
           }
        i--;
      }
      return 0;
}

 

Exit mobile version