Converting a number From One base to another

Converting a number From One base to another :



#include<bits/stdc++.h>
using namespace std;
int main()
{
    int base1,base2;
    int num;

    while(1){cout<<"Enter a number to convert(enter 0 to exit ): ";
    cin>>num;
    if(num==0)break;
    int x=num;
    cout<<"Enter the present base of this number: ";
    cin>>base1;
    cout<<"Enter a base in which you want to convert this number :";
    cin>>base2;
    int rem;
    int base=1;
    int num2=0;
    while(num>0)
    {
        rem=num%base2;
        num2=num2+rem*base;
        num=num/base2;
        base=base*base1;
    }
    cout<<x<<" after converted to  "<< base2 <<":"<<num2<<endl;
    cout<<"sajibtalukder2k16.blogspot.com"<<endl;}


}

 

 

 

 

 

 


Post a Comment

0 Comments