I need to know what is wrong in my code
#include
#include
using namespace std;
int btod(int n){
int s=0;
int b;
for(int i=0;i<=n;i++){
int p=n%10;
b=pow(2,i);
s=s+p*b;
n=n/10;
}
return s;
}
int main(){
int n;
cin>>n;
int w=btod(n);
cout<<w<<endl;
}
Write a program to convert binary to decimal using functions
Sir, Why can’t we use for loop in this code as I wrote it earlier.
basically what u were doing was running a loop for n times… but we just need to run a loop till n is not zero…
i hope its clear now?
Yess ,I understood completely
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.