Monday, September 19, 2011

#C_CODE: Base calculator


#include <stdio.h>

char * basecalc(int num,int base)
{
    static char buff[32];
    char *s = &buff[31];
    *s  = '\0';
    while(num)
    {
        *(--s) = "0123456789ABCDEF"[num%base];
        num /= base;
    }
    return s;
}

int main()
{
    int num,base;
    fputs("Enter the num: ",stdout);
    scanf("%d",&num);
    fputs("Enter the base: ",stdout);
    scanf("%d",&base);
    printf("The num %d in base %d is: %s\n",num,base,basecalc(num,base));
    return 0;
}

No comments: