- Binary to Decimal
Well, we know Binary is the number system containing only two digits or bits 0 and 1 and Decimal contains 10 digits (0-9).
#include<iostream>
using namespace std;
int BinDec(int num){
int bits = num;
int rem;
int result = 0;
int pow = 1;
while(bits > 0){
rem = bits % 10;
result += rem * pow;
pow = pow * 2;
bits = bits/10;
}
cout<<result;
}
int main(){
BinDec(1011);
}
2. Decimal to Binary
#include<iostream>
using namespace std;
int DecBin(int num){
int n = num;
int rem;
int result = 0;
int pow = 1;
while(n > 0){
rem = n % 2;
result += rem * pow;
n = n/2;
pow = pow * 10;
}
cout<<result;
}
int main(){
DecBin(5);
}