Binary to decimal in c++

 Binary to decimal in C++

Given a binary number as an integer N, convert it into decimal and print.

Input format :
An integer N in the Binary Format
Output format :
Corresponding Decimal number (as integer)
Constraints :
0 <= N <= 10^9
Sample Input 1 :
1100
Sample Output 1 :
12
Sample Input 2 :
111
Sample Output 2 :
7
C++ Code:
#include<iostream>
using namespace std;
int main ()
{
    int n, rem, dec = 0, b = 1;
    cin >> n;
    while (n > 0)
    {
        rem = n % 10;
        dec = dec + rem * b;
        b *= 2;
        n /= 10;
    }
    cout  << dec;
    return 0;
}



SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment