Convert binary to decimal (library functions and mathematical methods)

Title description
Knowing a binary number that only contains 0 and 1, and the length is not greater than 10, convert it to decimal and output it.

Input description
Input a binary integer n whose length is greater than 0 and not greater than 10

Output description
Output the converted decimal number, occupies one line

Sample input
110

Sample output
6

#include<iostream>

using namespace std;

int main()
{
    
    
    int n;
    while(cin>>n)
    {
    
    
        int m = 0, weight = 1;
        while(n != 0)
        {
    
    
            m += n % 10 * weight;
            weight *= 2;
            n /= 10;
        }
        cout<<m<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/KO812605128/article/details/115161199