codescracker


c

C Program to Convert Decimal to Hexadecimal



« Previous Program Next Program »


Decimal to Hexadecimal Conversion in C

To convert decimal number to hexadecimal number in C programming, you have to ask to the user to enter the decimal number to convert it into hexadecimal number to display the equivalent value in hexadecimal

C Programming Code to Convert Decimal to Hexadecimal

Following C program ask to the user to enter any number in decimal to convert it into hexadecimal, then display the result on the screen :

/* C Program - Decimal to Hexadecimal Conversion */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	long int decnum, rem, quot;
	char hexdecnum[100];
	int i=1, j, temp;
	printf("Enter any decimal number : ");
	scanf("%ld",&decnum);
	quot=decnum;
	while(quot!=0)
	{
		temp=quot%16;
		// to convert integer into character
		if(temp<10)
		{
			temp=temp+48;
		}
		else
		{
			temp=temp+55;
		}
		hexdecnum[i++]=temp;
		quot=quot/16;
	}
	printf("Equivalent hexadecimal value of %d is : \n",decnum);
	for(j=i-1; j>0; j--)
	{
		printf("%c",hexdecnum[j]);
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program convert decimal to hexadecimal

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test