Skip to content

Instantly share code, notes, and snippets.

@asd227695
Last active January 17, 2019 10:58
Show Gist options
  • Save asd227695/77618cec2182bcdcc42516ddd0183e34 to your computer and use it in GitHub Desktop.
Save asd227695/77618cec2182bcdcc42516ddd0183e34 to your computer and use it in GitHub Desktop.
Conversion of floating point decimal into other base system
#include<iostream>
using namespace std;
void convert(float num,float base)
{
int a[30];
int num1,k;
num1=num;
num=num-num1;
int t;
a[0]=num1%(int)base;
num1=num1/(int)base;
int i=1;
int c=0;
while(num1>=1)
{
a[i]=num1%(int)base;
num1=num1/(int)base;
i++;
c++;
}
for(int j=c;j>=0;j--)
{
if(a[j]>9){a[j]=a[j]+55;cout<<(char)a[j];}
else{cout<<a[j];}
}
int b[20];
i=0;
float temp;
do
{
temp=(num*base);
b[i]=(int)temp;
num=temp-b[i];
i++;
// cout<<"\nvalues after multi "<<temp<<endl;
if(temp==1){k=i;break;}
else{k=7;}
}while(i<20);
cout<<".";
for(int j=0;j<k-1;j++)
{
if(b[j]>9){b[j]=b[j]+55;cout<<(char)b[j];}
else{cout<<b[j];}
}
}
int main()
{
float num;
float base;
char ch;
do
{
cout<<"Enter the decimal number :";
cin>>num;
cout<<"\nEnter the base system you want to convert: ";
cin>>base;
convert(num,base);
cout<<"\nTo do the process again press y"<<endl;
cin>>ch;
}while(ch=='y');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment