# Program to convert Decimal to Hexadecimal

Problem statement:-  Program to convert Decimal to Hexadecimal.

## Data requirement:-    Input Data:- dec   Output Data:- Hex   Additional Data:-i, j, rem Program in C

Here is the source code of the C program to convert decimal number to hexadecimal number.

Code:

#include <stdio.h>
#include <math.h>
int
main ()
{
int dec,rem,i=0;
printf ("Enter Decimal number: ");
scanf ("%d", &dec);
char Hex[100];
while(dec!=0)
{
rem=dec%16;
//Convert Integer to char
if(rem<10)
Hex[i++]=rem+48;// 48 Ascii=0
else
Hex[i++]=rem+55;//55 Ascii=7
dec/=16;
}

int j;
for(j=i-1;j>=0;j--)
printf("%c",Hex[j]);
return 0;
}

Input/Output:
Enter Decimal number: 30

Program in C++

Here is the source code of the C++ program to convert decimal to hexadecimal using a while loop.

Code:

#include <iostream>
using namespace std;
int
main ()
{
int dec,rem,i=0;
cout<<"Enter Decimal number: ";
cin>>dec;
char Hex[100];
while(dec!=0)
{
rem=dec%16;
//Convert Integer to char
if(rem<10)
Hex[i++]=rem+48;// 48 Ascii=0
else
Hex[i++]=rem+55;//55 Ascii=7
dec/=16;
}

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

Input/Output:
Enter Decimal number: 1010

Program in Java

Here is the source code of the Java program to convert a decimal number to a hexadecimal number.

Code:

import java.util.Scanner;

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int dec,rem,i=0;
System.out.println("Enter Decimal number:");
dec=cs.nextInt();
char Hex[]=new char[100] ;
while(dec!=0)
{
rem=dec%16;
//Convert Integer to char
if(rem<10)
Hex[i++]=(char)(rem+48);// 48 Ascii=0
else
Hex[i++]=(char)(rem+55);//55 Ascii=7
dec/=16;
}

int j;
for(j=i-1;j>=0;j--)
System.out.print(Hex[j]);
cs.close();
}
}

Input/Output:
Enter Decimal number:
2014

Program in Python

Here is the source code of the Python Program program to convert decimal to hexadecimal without function.

Code:

i=0
dec=int(input("Enter Decimal number: "))
Hex=['0']*50
while dec!=0:
rem=dec%16;
#Convert Integer to char
if rem<10:
Hex[i]=chr(rem+48)#48 Ascii=0
i+=1
else:
Hex[i]=chr(rem+55) #55 Ascii=7
i+=1
dec//=16

for j in range(i-1,-1,-1):
print(Hex[j],end="")

Input/Output:
Enter Decimal number: 1212