Thursday, January 29, 2015

number system convertion : decimal to any base

#include < stdio.h >
#include < conio.h >
void convert(long int,int);
void main()
{
    int a,b;
    char ch;
    do
    {
        clrscr();

        printf("DECIMAL TO ANY NUMBER SYSTEM CONVERSION WITH BASE OF (2-16)\n\n");

        printf("Enter a decimal no (1-999999) : ");
        scanf("%ld",&a);

        printf("Enter base no : ");
        scanf("%d",&b);

        printf("\n\n%d  BASE  %d =  ",a,b);

        convert(a,b);
        printf("\n\n\nContinue with another no : (y/n) ? : ");

        ch=getch();
    }while(ch=='y'||ch=='Y');
}
void convert(long int a,int b)
{
    int i=24,j=0,d;
    long int c;
    char r[25],ref[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    c=a;

    while(j++<25 br="">        r[j]=' ';

    do
    {
        d=c%b;
        c=c/b;
        if(d>=10)
            r[i]=ref[d];
        else
            r[i]=ref[d];
        i--;
    }while(c!=0);

    j=0;

    while(j++<24 br="">        if(r[j]!=' ')
            printf("%c  ",r[j]);
}

No comments:

Post a Comment