on Leave a Comment

C++ Program to Convert Binary to Decimal

This article contains C++ Program to Convert Binary Number to Decimal Number.

C++ Program to Convert Binary Number to Decimal Number

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int bin,i=0,decnum=0,rem;
    cout<<"Enter a binary number: ";
    cin >>bin;
    while(bin!=0)
    {
        rem=bin%10;
        bin/=10;
        decnum=decnum+rem*pow(2,i);
        ++i;
    }
    cout<<"\nDecimal equivalent is "<<decnum;
    return 0;
}

OUTPUT:

Enter a binary number: 10001

Decimal equivalent is 17


0 comments:

Post a Comment