DEV Community

Shakeeb Shahid
Shakeeb Shahid

Posted on • Updated on

Using continue keyword to convert binary to decimal. No use of array, string or any pre-defined conversion. Complexity O(log n)

So this post is just to show a different approach to convert binary to decimal.
I would like the community to make this code better in terms of complexity. If possible 😜😝

using namespace std;
int main()

    int n;
    cin >> n;
    int count=0;
    int sum =0;
    for(int i=n;i>0;i=i/10)
       int rem = i%10;
            count =count +1;
        else if(rem==1)
        sum = sum + pow(2,count);
        count =count+1;
    cout << sum <<endl;
Enter fullscreen mode Exit fullscreen mode

Time Complexity : O(log n)
Correct me guys if I'm wrong!

Top comments (0)