Not getting Idea of code
Conversion (any to any)
hi @kumarakash121005
The formlua we have Learned so far only works when either the source base or destination Base is decimal.
The Logic is to First conver the corresponding given source base to decimal and then convert the decimal base to Given destination base.
In short the Question is Any Base to Decimal and then Decimal to Any Base.
refer this–>
#include<iostream>
using namespace std;
int conversion(int num, int sb, int db){
// Any Base to Decimal
int ans = 0;
int mult = 1;
while(num != 0){
int rem = num % 10;
ans = ans + rem * mult;
mult = mult * sb;
num = num / 10;
}
// Refresing the variables
mult = 1;
num = ans;
ans = 0;
//Decimal to Any number
while(num != 0){
int rem = num % db;
ans = ans + rem * mult;
mult = mult * 10;
num = num / db;
}
return ans;
}
int main() {
int src, dest,sn;
cin>>src>>dest>>sn;
cout<<conversion(sn, src, dest)<<endl;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.