#include <iostream>
using namespace std;
int main() {
int sumStore=0,base=1,bin,num,num1,rem;
cout<<"Enter a binary number : ";
cin>>num;
bin=num;
num1=num;
//Loop to check the number is binary or not
while(num!=0)
{
if((num%10!=0 )&& (num%10!=1))
{
cout<<"The number "<<bin<<" is not binary\n";
return 0;
}
num=num/10;
}
//if number is binary then this loop will execute
while(num1!=0)
{
rem=num1%10;
sumStore=sumStore+(rem*base);
base=base*2;
num1=num1/10;
}
cout<<"\nThe decimal Equivalent of binary No "<<bin<<" is = "<<sumStore<<endl;
return 0;
}
No comments:
Post a Comment