Binary To HexaDecimal C Program

Binary Number: Binary number system has base 2. That means it consts of two numbers (0 and 1) only.

HexaDecimal Number: HexaDecimal number system has base 16. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a–f) to represent values ten to fifteen. For example, the hexadecimal number 2AF3 is equal, in decimal, to (2 × 163) + (10 × 162) + (15 × 161) + (3 × 160) or 10995.

Binary To HexaDecimal C Program:


#include <stdio.h>

int main()
{
    int hex[1000], i=1, j=0, rem, dec = 0, bin;

    printf("Enter a Binary Number: ");
    scanf("%d",&bin);

    while(bin>0){
        rem = bin % 2;
        dec = dec + (rem*i);
        i = i * 2;
        bin = bin / 10;
    }

    /* At this state our input number is converted into Decimal Number */
    i = 0;
    while(dec!=0){
       hex[i] = dec % 16;
       dec = dec/16;
       i++;
    }

    printf("Equivalent HexaDecimal value: ");
    for(j=i-1;j>=0;j--){
        if(hex[j]>9)
            printf("%c",hex[j]+55);
        else
            printf("%d",hex[j]);
    }
    return 0;
}

0 comments :

Post a Comment

Spam comments will be deleted. :)

 
Loading...
TOP