## Algorithm

Algorithm to Convert Binary to Decimal:

1. Start
2. Read the binary number as input.
3. Initialize decimal_number to 0 and power to 0.
4. Repeat the following steps until all digits are processed:
a. Extract the rightmost digit of the binary number.
b. Multiply the extracted digit by 2^power.
c. Add the result to decimal_number.
d. Divide the binary number by 10.
e. Increment power by 1.
5. Display the decimal_number as the result.
6. End

Algorithm to Convert Decimal to Binary:

1. Start
2. Read the decimal number as input.
3. Initialize binary_number to 0 and place_value to 1.
4. Repeat the following steps until the decimal number is greater than 0:
a. Find the remainder when the decimal number is divided by 2.
b. Multiply the remainder by place_value.
c. Add the result to binary_number.
d. Divide the decimal number by 2.
e. Multiply place_value by 10.
5. Display the binary_number as the result.
6. End

## Code Examples

### #1 Code Example- C++ Programing to Convert Binary Number to Decimal

```Code - C++ Programming```

``````// convert binary to decimal

#include <iostream>
#include <cmath>

using namespace std;

// function prototype
int convert(long long);

int main() {
long long n;
cout << "Enter a binary number: ";
cin >> n;
cout << n << " in binary = " << convert(n) << " in decimal";
return 0;
}

// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;

while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}

return dec;
}``````
Copy The Code &

Output

cmd
Enter a binary number: 1101
1101 in binary = 13 in decimal

### #2 Code Example- C++ Programing to convert decimal number to binary

```Code - C++ Programming```

``````// convert decimal to binary

#include <iostream>
#include <cmath>
using namespace std;

long long convert(int);

int main() {
int n, bin;
cout << "Enter a decimal number: ";
cin >> n;
bin = convert(n);
cout << n << " in decimal = " << bin << " in binary" << endl ;
return 0;
}

long long convert(int n) {
long long bin = 0;
int rem, i = 1;

while (n!=0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}

return bin;
}``````
Copy The Code &

Output

cmd
Enter a decimal number: 13
13 in decimal = 1101 in binary