C Program to Convert Decimal to Hexadecimal

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

Run1:
Run2:

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...