It is very well known how to convert a decimal number into binary format, i.e. base 2. The method can be easily generalized for any positive integral base. In fact, it is not essential that a base be a positive integer, rather it can be negative, fractional and even imaginary. In a previous article I have discussed how to convert a decimal number to a fractional base (link is given at the bottom). In this article I have provided a C code for converting a decimal number into any positive integral base.

The C program

The program takes the number and the base from user as input and displays the result. The input number can be negative, positive, integral or fractional but the base must be integer greater than 2 since it is meaningless to convert a number into base 1 or 0.


#include<stdio.h>
#include<math.h>
void conversion(int *array,double x, int b,int length);
void display(int *ar,int l);


/**** ---- Main ---- ****/
int main()
{
int i,b,l_arr,length,*arr,sign;
float x;
/* Taking inputs */
printf("Enter number: ");
scanf("%f",&x);
do{
printf("Enter base: ");
scanf("%d",&b);
if(b<2) printf("Error: Base must be greater than 2.\n\n");
}while(b<2);


if(x<0) {x=-x; sign=1;} // Case of negative number //
l_arr=ceil((digit_count((int)x))*log(10)/log(b)); // Lenght of array //
arr=(int*)calloc((l_arr+10),sizeof(int)); // Initializing array //
conversion(arr,x,b,l_arr); // Performing conversion //


/* Displaying results */
if(sign==1)
printf("\n%f in base %d is\n\n-",-x,b);
else
printf("\n%f in base %d is\n\n",x,b);
display(arr,l_arr);
getch();
}
/**** ---- End of main ---- ****/


/**** ---- Function for performing conversion ---- ****/
void conversion(int *array,double x, int b,int length)
{
int i,intgr;
double frac;


intgr=(int)x;
frac=x-intgr;
/* Converting integral part */
for(i=length-1;i>=0;i--)
{
array[i]=intgr%b;
intgr/=b;
if(intgr==0)
break;
}
/* Converting fractiona part */
for(i=length;i<length+10;i++)
{
frac*=b;
array[i]=(int)frac;
frac-=array[i];
if(frac==0)
break;
}
}
/**** ---- End of conversion ---- ****/


/**** ---- Function for counting digits ---- ****/
int digit_count(int n)
{
int digit_no=0,i;
for(i=n;i>0;i/=10)
digit_no++;


return(digit_no);
}
/**** ---- End of digit_count ---- ****/


/**** ---- Function for displaying result ---- ****/
void display(int *ar,int l)
{
int i;
for(i=0;i<l+10;i++)
{
if(i==l)
printf(".");
printf("%d ",ar[i]);
}
printf("\n");
}
/**** ---- End of display ---- ****/

The program uses three functions “conversion”, “digit_count” and “Display”. Among them “conversion” is the most important which actually carries out the conversion process using array. “digit_count” is used for calculating number of digits of an integer and “display” just prints the result. It is also quite straight forward to convert a decimal number into negative base.

How to Convert a Decimal Number to Any Fractional Base: A Tutorial

Some Useful Mathematica Commands on Matrix Algebra: A Tutorial

If you want to join ExpertsColumn click here.


Promote This Column on Other Sites: