Binary to Decimal

#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

Popular posts from this blog

কন্টেস্ট রিলেটড কিছু বিষয়

Efficiently getting all divisors of a given number

Print sum of prime divisor