RUN
C C++ JAVA PYTHON SQL HTML CSS DSA Robotics AWS CODING INTERVIEW PREPARATION

Made with  & Code

# C++ program to convert decimal to hexadecimal

Written By -

## Convert Decimal to Hexadecimal program in C++

A decimal number is taken as input and converted it into its hexadecimal equivalent.

Example:

Decimal number: 77

Corresponding hexadecimal number: 4D

Decimal number: 50

Corresponding hexadecimal number: 32

### Algorithm:

1. A decimal number is taken as input.
2. The number is divided by 16 and the remainders are stored.
3.  The result is printed in reverse order.

Code:

``````#include<iostream>
using namespace std;

int main()

{

long int dno, temp;

char hex;

int j, remainder, i=1;

cout<<"Enter Decimal Number : ";

cin>>dno;

temp = dno;

while(temp!=0)

{

remainder = temp%16;

if(remainder<10)   // Converts integer into character

{

remainder = remainder + 48;

}

else

{

remainder = remainder + 55;

}

hex[i++] = remainder;

temp = temp/16;

}

cout<<"\nHexadecimal Number corresponding to Decimal Number : ";

for(j=i-1; j>0; j--)

{

cout<<hex[j];

}

return 0;

}``````

Report Error/ Suggestion

Related Posts:

Online Compilers

CopyRight © 2020

CopyRight © 2020