programming-examples/c++/Basic/Binary to Decimal.cpp

19 lines
527 B
C++
Raw Normal View History

2019-11-18 14:44:36 +01:00
#include < iostream.h >
void main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
cout<<"Enter a binary number(1s and 0s) \n";
cin>>amp num;
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
num = num / 10 ;
base = base * 2;
}
cout<<"The Binary number is ="<<binary_val<<" \n";
cout<<"Its decimal equivalent is ="<<decimal_val<<" \n";
}