INDEX

Decimal to hexadecimal conversion in c

1. C code to convert decimal to hexadecimal

#include<stdio.h>
int main(){
long int decimalNumber,remainder,quotient;
int i=1,j,temp;

printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);

quotient = decimalNumber;

while(quotient!=0){
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

quotient = quotient / 16;
}

printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)

return 0;
}

Sample output:

Enter any decimal number: 45
Equivalent hexadecimal value of decimal number 45: 2D

2. Easy way to convert decimal number to hexadecimal number:

#include<stdio.h>
int main(){

long int decimalNumber;

printf("Enter any decimal number: ");
scanf("%d",&decimalNumber);

printf("Equivalent hexadecimal number is: %X",decimalNumber);

return 0;
}

Sample output:

Enter any decimal number: 45
Equivalent hexadecimal number is: 2D

Hexadecimal number system: It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F.

Decimal number system:
It is base 10 number system which uses the digits from 0 to 9

Decimal to hexadecimal conversion method:

Following steps describe how to convert decimal to hexadecimal

Step 1: Divide the original decimal number by 16
Step 2: Divide the quotient by 16
Step 3: Repeat the step 2 until we get quotient equal to zero.

Equivalent binary number would be remainders of each step in the reverse order.

Decimal to hexadecimal conversion example:

For example we want to convert decimal number 900 in the hexadecimal.

Step 1:  900 / 16  Remainder : 4 , Quotient : 56
Step 2:   56 / 16  Remainder : 8 , Quotient : 3
Step 3:    3 / 16  Remainder : 3 , Quotient : 0

So equivalent hexadecimal number is: 384
That is (900)10 = (384)16

6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

Anonymous said...

please write the program for follwing
if u enter number between 2 to 9 it will display like
5=1,1,1,1,1
5=1,1,1,2
5=1,1,3
5=1,4
5=2,1,1,1
5=2,2,1
5=3,1,1
5=4,1

Anonymous said...

hey..can someone help me?
I need to create a program that will convert binary to decimal,octal, and hexadecimal.
ex.
enter binary number:

output:
decimal=
octal=

fayz said...

hey the above program will not give the currect hexa decimal output.....

Anonymous said...

Question, where do you get that 48 and 55 from?
while(quotient!=0){
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

quotient = quotient / 16;
}

Anonymous said...

What the hell are you doing man.

USE SCANF("%d",&n);
printf("%x",n);

that 's it stupid

Unknown said...

temp =temp + 48; (this is used for converting the digits from 0 to 9 to its ASCII value)
i.e.
ASCII value of the digit = the value of the digit + ASCII value of 0(zero)

temp = temp + 55; (this is used for converting the digits from A(10) to F(15) to its ASCII value) i.e.
ASCII value of the digit = the value of the digit-10 + ASCII value of 'A'.
Here -10 is done to correct the ASCII to the corresponding digit.

Anonymous said...

It's an exercise buddy, relax

Unknown said...

Write a c program to convert hexadecimal number to decimal number. please send this programming

Anonymous said...

ascII value of A is 65,so 55 should be replaced by 65

Unknown said...

only change base any system convet u

#include
#include
using namespace std;
int main()
{
int deci=900,hex=0,rem,i=1;
while(deci>0)
{
rem=deci%16;
deci=deci/16;
hex=hex+(i*rem);
i=i*10;

}
cout<<hex;

getch ();
return 0;
}

Anonymous said...

The easy way is easy hhhhhhh
Thank u alot

my blog said...

void To(long long num,char *buff,int base)
{
if(buff==NULL) return;
long long m=0,no=num,i=1;

while((no/=base)>0) i++;
buff[i]='\0';

no=num;
while(no>0)
{
m=no%base;
no=no/base;
buff[--i]=(m>9)?((base==16)?('A' + m - 10):m):m+48;
}
}

Unknown said...

That's exactly what it does

Unknown said...

Write a program in c ++ hundred decimal and display the total result.

Unknown said...

thanks, nice post

Unknown said...

Enter number x.print 1 to 100 and what number between the range devide by x to get integer.

Unknown said...

Enter number x.print 1 to 100 and what number between the range devide by x to get integer.