Skip to content

Instantly share code, notes, and snippets.

@aligalehban
Created June 21, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aligalehban/f55323d55c4c60cf729459a7cb4e8df1 to your computer and use it in GitHub Desktop.
Save aligalehban/f55323d55c4c60cf729459a7cb4e8df1 to your computer and use it in GitHub Desktop.
convert binary to decimal in c++ source code - www.alighalehban.com
#include <iostream>
#include <cmath>
using namespace std;
int convertBinaryToDecimal(long long);
int main()
{
long long n;
cout << "Enter a binary number: ";
cin >> n;
cout << n << " in binary = " << convertBinaryToDecimal(n) << "in decimal";
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment