Wednesday, 26 December 2012

Any base to any base conversion


The Program for any base to any base conversion:

This program converts a given number in any base(2,8,10,16) to any base that user wants.here we use two functions one is  "other_to_decimal" which is for converting a number from any base to decimal,; and other is "decimal_to_other" which is for converting a number from decimal to any base.. 

//Any Base to Any Base...
#include<stdio.h>
#include<conio.h>
long signed int other_to_decimal(long signed int,long signed int);
long signed int decimal_to_other(long signed int,long signed int);
void main()
{
long signed int inputbase,outputbase,inputnumber,outputnumber,s;
clrscr();
printf("Enter the number and its base:");
scanf("%ld%ld",&inputnumber,&inputbase);
printf("Enter the base you want to convert the number to:");
scanf("%ld",&outputbase);
if(outputbase==10)
{
outputnumber=other_to_decimal(inputnumber,inputbase);
}

else
{

if(inputbase==10)
{
outputnumber=decimal_to_other(inputnumber,outputbase);
}
else
{
s=other_to_decimal(inputnumber,inputbase);
outputnumber=decimal_to_other(s,outputbase);
}
}
printf("The converted number is %ld",outputnumber);
getch();
}
long signed int other_to_decimal(long signed int inputnumber,long signed int inputbase)
{
long signed int k,op,s,p;
op=0;
k=inputnumber;
p=1;
while(k!=0)
{
s=k%10;
k=k/10;
op=op+s*p;
p=p*inputbase;
}
return op;
}
long signed int decimal_to_other(long signed int outputnumber,long signed int outputbase)
{
long signed int op=0,p,k,s;
k=outputnumber;
p=1;
while(k!=0)
{
s=k%outputbase;
k=k/outputbase;
op=op+s*p;
p=p*10;
}
return op;
}

and the output is like:


4 Responses to “Any base to any base conversion”

Unknown said...
31 October 2013 at 07:45

#include
#include
#include

void main()
{
int b[20],i,j,sum1=0,base=0,base2=0,count=0,f=0,decimal=0,r=0,temp=0,l=0;
char a[20],c[20],d[20],ch='y';
float sum2=0,sum=0,fdecimal=0,frac=0;
while(ch=='y' || ch=='Y')
{
system("cls");
sum=0; sum1=0; sum2=0; f=0; base=0; base2=0; frac=0; decimal=0; r=0; fdecimal=0; l=0; temp=0; count=0;
printf("2.for Binary\n3.for Turnery\n4.for Quaternary\n5.for Quinary\n8.for Octal\n10.for Decimal\n12.for Duodecimal\n16.for Hexa-Decimal\n");
printf(" Enter the sourse no.-");
scanf("%d",&base);
if(base>1 && base<17)//base==2 || base==3 || base==4 || base==5 || base==8 || base==10 || base==12 || base==16)
{
printf("\n\n In which number system u want to convert\n\n");
printf("2.for Binary\n3.for Turnery\n4.for Quaternary\n5.for Quinary\n8.for Octal\n10.for Decimal\n12.for Duodecimal\n16.for Hexa-Decimal\n");
printf(" Enter destination no.-");
scanf("%d",&base2);
if(base2>1 && base2<17)//base2==2 || base2==3 || base2==4 || base2==5 || base2==8 || base2==10 || base2==12 || base2==16)
{
printf("enter number-");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]!='.')
{
if(a[i]>47 && a[i]<58)
{
b[i]=a[i]-48;
if(b[i]>=base || b[i]<0)
{
f=1;
break;
}
}
else if(toupper(a[i])>64 && toupper(a[i])<71)
{
b[i]=toupper(a[i])-55;
if(b[i]>=base || b[i]<0)
{
f=1;
break;
}
}
else
{
f=1;
break;
}
}
}
if(f==1)
printf("Invalid Number\n");

else
{
i=0;
while(a[i]!='.' && a[i]!='\0')
{
count++;
i++;
}
for(i=count-1,j=0;i>=0;i--,j++)
{
sum1+=b[i]*pow((double)base,j);
}
// printf("Integer Part=%d\n",sum1);
if(a[count]=='.')
{
for(i=count+1,j=-1;a[i]!='\0';i++,j--)
{
sum2+=b[i]*pow((double)base,j);
}
// printf("Fraction Part=%f\n",sum2);
}

sum=sum1+sum2;
printf("\nDecimal Number=%f\n\n",sum);

decimal=sum;
fdecimal=sum-decimal;
i=0;
while(decimal!=0)
{
r=decimal%base2;
if(r>=0 && r<10)
c[i]=r+48;
else if(r>9 && r<16)
c[i]=r+87;
decimal=decimal/base2;
i++;
}
for(l=i-1,j=0;l>=0;l--,j++)
{
d[j]=c[l];
}
d[j]='.';
for(l=j+1;l=0 && temp<10)
d[l]=temp+48;
else if(temp>9 && temp<16)
d[l]=temp+87;
}
d[l]='\0';
printf("Answer=%s\n",d);
}
}
else
printf("Invalid Choice\n");
}
else
printf("Invalid Choice\n");
printf("Do u Want To continue?? then press Y:-");
scanf(" %c",&ch);
}
getch();
}


tharun said...
27 August 2018 at 04:38

/*program to convert a number of one base to another*/
#include
#include
#include
void main()
{
int pres_num,pres_base,ano_base,n,sum=0,x,x1,x3,x2;
long int sum1=0;
printf("\n enter given number:");
scanf("%d",&pres_num);
printf("\n its base:");
scanf("%d",&pres_base);
printf("\n another base:");
scanf("%d",&ano_base);
n=log(pres_num)+1;//finding number digits the given number
x1=1;
while(pres_num!=0)//converting the given number into decimal
{
x=pres_num%10;
sum=sum+(x*x1);
pres_num=pres_num/10;
x1=x1*pres_base;
}
printf("sum:%d",sum);
x3=1;
while(sum!=0)//converting the calculated decimal to respective bbase number
{
x2=sum%ano_base;
sum1=sum1+(x2*x3);
sum=sum/ano_base;
x3=x3*10;
}
printf("\n converted number:%d",sum1);
getch();
}


Unknown said...
23 January 2019 at 12:34

why people put these garbages here?


vedahbackman said...
25 February 2022 at 23:22

Casino Poker Texas: Free Play, No Deposit Bonus Codes
Find the best 유흥 후기 casino poker sites online. 브라밝기조절 for the best payouts and highest quality 암호 화폐 종류 online poker games. 안전 바카라 사이트 Get 실시간 배팅 사이트 $10 free, no deposit required!


Post a Comment

Recent Comments

All Rights Reserved Assignments on C language