#include <bits/stdc++.h>
using namespace std;
int binarytodec(long long n)
{
int dec_number=0;
int i=0, rem;
while(n!=0)
{
rem=n%10;
n=n/10;
dec_number=dec_number+rem*pow(2,i);
i++;
}
return dec_number;
}
int main ()
{
long long n;
cin >> n;
cout << binarytodec(n) << endl;
return 0;
}
Another Example:
#include <bits/stdc++.h>
using namespace std;
int main ()
{
char str[100];
cin.getline(str, 100);
int value=0;
int len = strlen(str)-1;
for(int i=0; str[i]!='\0'; i++)
{
value = value + (str[i]-'0')*pow(2, len);
len--;
}
cout << value << endl;
return 0;
}
Comments
Post a Comment