Write a C++ Program to Convert Binary Number to Decimal and vice-versa

C++ Program to Convert Binary Number to Decimal and vice-versa

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn C++ programming concepts. You can learn C++ language at your own speed and time. One can learn concepts of C++ language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

C++ Program to Convert Binary Number to Decimal and vice-versa
C++ Program to Convert Binary Number to Decimal and vice-versa

Write a C++ Program to Convert Binary Number to Decimal and vice-versa

PROGRAM: C++ Program to Convert Binary Number to Decimal and vice-versa

/* C++ Program to Convert Binary Number to Decimal and vice-versa */

// 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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *