Friday 15 January 2016

Solution of URI 1957 :: Converting to Hexadecimal


Before seeing the solution make sure that you tried enough. Don’t paste the whole code, just find out the logic. If you stuck in trouble, just inform me on comment.

/**Bismillahir Rahmanir Rahim.**/

#include <stdio.h>
#include <string.h>
int main()
{
    long long int decimal;
    char hexa[20];
    scanf("%lld", &decimal);
    long long int i=0, mod, j, lnth;
    while(decimal>0)
    {
        mod = decimal % 16;
        decimal /= 16;
        if(mod<=9)
            hexa[i] = mod+48;
        else if(mod==10)
            hexa[i] = 'A';
        else if(mod==11)
            hexa[i] = 'B';
        else if(mod==12)
            hexa[i] = 'C';
        else if(mod==13)
            hexa[i] = 'D';
        else if(mod==14)
            hexa[i] = 'E';
        else if(mod==15)
            hexa[i] = 'F';
        i++;
    }
    hexa[i] = '\0';
    lnth = strlen(hexa);
    for(j = lnth-1; j>=0; j--)
        printf("%c", hexa[j]);
    printf("\n");
    return 0;
}

1 comment:

Note: only a member of this blog may post a comment.