This post contains 47+ CPP Binary to Decimal Converter Examples with Source Codes. All the Converters are made using C++ Programming Language.
You can use the source codes of these converters for your own personal or educational use with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Eric Macheru
Made by Eric Macheru. A simple C++ Binary to Decimal Converter. ( Source )
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
long binaryNumber, decimalNumber = 0, j = 1, remainder;
cout << "\n\n Convert a binary number to decimal number:\n";
cout << "-----------------------------------------------\n";
cout << " Input a binary number: ";
cin>> binaryNumber;
while (binaryNumber != 0)
{
remainder = binaryNumber % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryNumber = binaryNumber / 10;
}
cout<<" The decimal number: " << decimalNumber<<"\n";
}
;
2. By Michael Ngwerume
Made by Michael Ngwerume. ( Source )
#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;
}
3. By Simon
Made by Simon. ( Source )
/*
A simple console app started by Simon McGraw on 2/1/21.
The goal of this program is to allow a user to enter a value in binary, and have it
converted and output to the console as a decimal value.
*/
#include <iostream>
#include <string>
#include <cmath>
int convertToDecimal(const std::string inString);
int main()
{
char choice = 'y';
while (choice != 'n') {
int decimal = 0;
std::string inBinary;
std::cout << "Please enter up to 8 binary digits now: ";
std::cin >> inBinary;
//very basic input validation
bool invalidEntry = 0;
do
{
for (int i = 0; i < inBinary.length(); i++) {
if (inBinary[i] != '1' && inBinary[i] != '0') {
invalidEntry = 1;
}
}
if (invalidEntry == 1) {
std::cout << "Error: Invalid digit found in string of binary digits, \nplease re-enter up to 8 binary digits: ";
std::cin.clear();
std::cin.ignore(200, '\n');
std::cin >> inBinary;
}
} while (invalidEntry != 0);
decimal = convertToDecimal(inBinary);
std::cout << "\nYou entered: " << inBinary << std::endl;
std::cout << "In decimal form that is: " << decimal << std::endl;
std::cout << "Would you like to enter another binary number? (y for yes, n for no): ";
std::cin >> choice;
}
}
/*
Takes the incomming string and converts it to a decimal value, stored as an int, then returns said value.
Pre: Incomming string has already been validated and does not confirm any digits other than a 1 or a 0, IE
it's actually a binary number.
*/
int convertToDecimal(const std::string inString)
{
int bLength = inString.length();
int decimal = 0;
for (int i = 0; i < bLength; i++) {
if (inString[bLength - (i + 1)] != '0') {
decimal += pow(2, i);
}
}
return decimal;
}
4. By Joel Buenrostro
Made by Joel Buenrostro. A simple converter of binary numbers to decimals. ( Source )
#include <iostream>
using namespace std;
int main()
{
long bin, dec = 0, rem, num, base = 1;
cout << "Enter binary number :";
cin >> num ;
while ( num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal number is :" << dec << endl ;
return 0;
}
/*
Open source code to modify, distribute, copy, steal and share.
Thanks for the visit.
Contact me via Twitter: @esteGeek
*/
5. By Handerson
Made by Handerson. Converts a 4 bit binary number into decimal. ( Source )
/****************************************************************
** Program Filename: binary_to_decimal.cpp
** Author: R. Hayden Anderson
** Date: 9/27/16
** Description: converts a 4 bit binary number into decimal
** Input: 4 bit binary number
** Output: the 4 bit binary into decimal
*****************************************************************/
#include <iostream>
using namespace std;
int main (){
cout << "\nPlease enter a 4 bit binary number" << endl;
int remainder; //holds remainder after modulo
int input; //takes original input
int output = 0; //running total for the conversion (starts at 0)
cin >> input; //gets 4 bit number from user
/* First iteration, check 4 bit number for the right most bit */
remainder = input % 10; //checks the "1's" place for a present bit
output = remainder; //adds the 1's place to the running total
input = (int)input/10; //no longer need 1's place and divides it away
/* Second iteration, check 4 bit number for the bit representing 2 */
remainder = input % 10; //checks the 2's place for a present bit
output = output + (remainder * 2); //adds the 2's place bit if present
input = (int)input /10; //divides away the 2's place
/* Third iteration, check 4 bit number for the bit representing 4 */
remainder = input % 10; //checks the 4's place bit for a present bit
output = output + (remainder * 4); //adds 4 to running total if present
input = (int)input /10; //divides the 4's place bit away
/* Fourth iteration, check 4 bit number for the bit representing 8 */
remainder = input % 10; //checks for bit in the 8's bit slot
output = output + (remainder * 8); //adds 8 to running total if bit is present
/*print decimal number*/
cout << "decimal: " << output << endl;
return 0;
}
6. By Saksham Jaswal
Made by Saksham Jaswal. ( Source )
#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;
}
7. By William Then
Made by William Then. Input binary string, change to Octal & Decimal. ( Source )
//#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
int main()
{
using namespace std;
char binary_string[33];
unsigned short int number;
cout << "Enter your Binary number now..." << endl;
cin >> binary_string;
number = stoi(binary_string, nullptr, 2);
cout << setbase(16);
cout << "Hexadecimal: " << number << endl;
cout << setbase(10);
cout << "Decimal: " << number << endl;
cout << setbase(8);
cout << "Octal: " << number << endl;
//system("PAUSE");
cin.clear();
cin.ignore(32767, '\n');
cin.get();
return 0;
}
8. By Abhishek Kumar
Made by Abhishek Kumar. ( Source )
#include <iostream>
#include<math.h>
using namespace std;
int binary2decimal(int n){
int decimal=0;
int x=0;
while(n>0){
decimal+=(n%10)*pow(2,x);
n/=10;
x++;
}
return decimal;
}
int main() {
int n;
cin>>n;
cout<<binary2decimal(n);
return 0;
}
9. By Ravi Kumar
Made by Ravi Kumar. ( Source )
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int n,num;
int ans=0;
int i=0;
cin>>num;
n=num;
while(n!=0)
{
int bit=n%10;
if(bit==1)
{
ans=(bit*pow(2,i))+ans;
}
i++;
n=n/10;
}
cout<<"Decimal of "<< num << " is " << ans;
return 0;
}
10. By Vivek Raj
Made by Vivek Raj. ( Source )
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
string s;
int t[100],v=0;
cin>>s;
int q=s.length();
for(int i=0;i<s.length();i++)
{
q--;
t[i]=s.at(i);
t[i]=t[i]-48;
v=v+t[i]*pow(2,q);
}
cout<<v;
return 0;
}
11. By Akash Pal
Made by Akash Pal. This program converts a Binary number into decimal first then to an octal number. ( Source )
#include <iostream>
using namespace std;
int main() {
long binary;
int remainder;
long decimal = 0, octal = 0,i=1;
cout<<"Enter a binary number :";
cin>>binary;
cout << binary << endl;
while(binary != 0){
remainder = binary%10;
decimal = decimal + (remainder*i);
binary=binary/10;
i=i*2;
}
cout <<"Decimal : " <<decimal << endl;
i=1;
while(decimal != 0){
remainder = decimal%8;
octal = octal + (remainder*i);
decimal=decimal/10;
i=i*10;
}
cout << "Octal Number :" << octal;
return 0;
}
12. By Shaban Khan
Made by Shaban Khan. ( Source )
#include<iostream>
using namespace std;
int btod(int n){
int ans=0;
int x=1;
while(n>0){
int ld= n%10;
ans += x*ld;
x*=2;
n=n/10;
}
return ans;
}
int main( ){
int n;
cin>>n;
cout<<btod(n)<<endl;
}
13. By Romesh Kumar Tripathi
Made by Romesh Kumar Tripathi. To use this programme Enter any binary number and get Decimal Conversion of that binary Number. ( Source )
#include <iostream>
#include<math.h>
using namespace std;
class Binary{
private:
char binary_array[16];//to store 16 bit array of binary digit
int count_character;
int decimal_data;
public:
void binaryInput();
void processBinaryData();
void outDecimalResult();
};
void Binary::binaryInput(){
int i;
count_character=0;
cin>>binary_array;
for(i=0;binary_array[i]!='\0';i++)
{
count_character+=1;
}
processBinaryData();
}
void Binary::processBinaryData(){
int i;
decimal_data=0;
for(i=count_character;i>=0;i--){
if(binary_array[i]=='1')
{
decimal_data=decimal_data+pow(2,(count_character-(i+1)));
}
}
outDecimalResult();
}
void Binary::outDecimalResult(){
cout<<"\nDecimal Result="<<decimal_data<<endl;
}
int main(){
Binary obj;
obj.binaryInput();
return 0;
}
14. By sharmaparnika
Made by sharmaparnika. ( Source )
#include<iostream>
#include<math.h>
using namespace std;
int main() {
int n;
cout<<"Enter binary number: ";
cin>>n;
int i=0, ans=0;
while(n!=0){
int digit=n%10;
if(digit==1){
ans=ans+pow(2,i);
}
i++;
n=n/10;
}
cout<<"Required decimal number is: "<<ans;
}
15. By Swapnil More
Made by Swapnil More. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int i,d=0,r,j=0;
cout<<"Enter a binary number: ";
string arr;
getline(cin,arr);
cout<<arr<<endl;
for(i=arr.length()-1;i>=0;i--){
r=arr[i];
r-=48;
d += r*pow(2,j);
j++;
}
cout<<arr<<" in decimal form is "<<d;
return 0;
}
16. By RajaAsyraf
Made by RajaAsyraf. Binary to Decimal Converter using C++. ( Source )
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int sum = 0;
int num;
int counter =0;
int power = 1;
int remainder = 0;
cout<<"8-bit Binary Number = ";
cin>> num;
bool valid = true;
bool notvalid = true;
while(notvalid)
{
remainder = num%10;
if(remainder==1 || remainder ==0)
{
sum = sum + (remainder*power);
}
else
{
cout<<"PLease insert binary number!";
return 0;
}
power = power*2;
counter = counter +1;
num=num/10;
if(counter ==8)
{
notvalid = false;
}
}
if (valid)
{
cout<<"Decimal Number = " <<sum;
}
cout << endl << endl;
system ("pause");
}
17. By Ravi Kumar
Made by Ravi Kumar. ( Source )
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int number=0,remainder,binary,i=0;
cin>>binary;
cout<<"You input ="<<binary;
while(binary>0)
{
remainder=binary%10;
binary/=10;
number+=remainder*pow(2,i);
i++;
}
cout<<endl<<"binary equivalent :"<<number;
return 0;
}
18. By Sagar Sikchi
Made by Sagar Sikchi. ( Source )
#include <iostream>
using namespace std;
int main() {
int n, remainder, temp, decimal = 0, base = 1;
n = 1010;
cout << "The binary number is: " << n << endl;
temp = n;
while(temp) {
remainder = temp % 10;
decimal = decimal + remainder * base;
base = base * 2;
temp = temp / 10;
}
cout << "The decimal equivalent of binary number is: " << decimal << "\n";
return 0;
}
19. By Dawka
Made by Dawka. ( Source )
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void change(long a){
auto temp = a;
int length = 0;
while(a!=0){
a /= 10;
length++;
}
int b[length];
for(int i=0; i<length; i++){
b[i] = temp%10;
temp /= 10;
}
int s=0, n=0;
for(int i=0; i<length; i++){
s += b[i]*pow(2,n); //i
n++;
}
cout << s;
}
int main() {
long binary;
cout << "Input a binary number: ";
cin >> binary;
cout << endl;
change(binary);
return 0;
}
20. By Kevin
Made by Kevin. C ++ program that converts binary values to decimals. ( Source )
// C++ program to convert binary to decimal by Kevin Shera
#include <iostream>
using namespace std;
// Function to convert binary to decimal
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
// Principal function
int main()
{
system("color A");
char option;
do{
int num;
cout<<"\nEnter the binary value to convert: ";
cin>>num;
cout << binaryToDecimal(num) << endl;
cout<<endl<<"Choose the most suitable option: "<<endl;
cout<<"c - continue and repeat the program "<<endl;
cout<<"e - finish and close the program "<<endl;
cout<<"p - clean the console and repeat the program ";
do{
cout<<endl<<"Choice: ";
cin>>option;
} while((option!='c')&&(option!='e')&&(option!='p'));
if (option=='p')
{
option='c';
system("cls");
}
} while (option=='c');
return 0;
}
21. By khadeeja Shamna
Made by khadeeja Shamna. ( Source )
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int dec=0,i=0,d;
long int bin;
cout<<"enter the binary number:";
cin>>bin;
cout<<bin;
do{
d=bin%10;
dec=dec+d*pow(2,i);
++i;
bin=bin/10;
}while(bin>0);
cout<<"\nThe decimal number is:"<<dec;
return 0;
}
22. By proGAMBLER
Made by proGAMBLER. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
// function prototype
int convert(int);
int main() {
int n;
cout << "Enter a binary number: ";
cin >> n;
cout << n << " in binary = " << convert(n) << " in decimal";
return 0;
}
// function definition
int convert(int n) {
int dec = 0, i = 0, rem,result=1;
while (n!=0) {
rem = n % 10;
n /= 10;
while (i!=0) {
result*=2;
i--;}
dec+=rem*result;
++i;
}
return dec;
}
23. By ROHIT KANOJIYA
Made by ROHIT KANOJIYA. ( Source )
#include<iostream>
#include<cmath>
using namespace std;
/*
This Code Convert the Binary Number to Decimal Number.
For Example:
Input: 101
Output: 5
*/
int BinarytoDecimal(long int n);
int main()
{
long int num;
// cout<<"Enter a Binary Number: ";
cin>>num;
cout<<"Decimal Number of "<<num<<" : "<<BinarytoDecimal(num);
return 0;
}
int BinarytoDecimal(long int n)
{
int i=0,r,decNum=0;
while(n>0)
{
r=n%10;
decNum=decNum + r * pow(2,i);
i++;
n=n/10;
}
return decNum;
}
24. By Vijay Patidar
Made by Vijay Patidar. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
int main() {
long binary,i=0,b,r,decimal=0;
cout<<"Enter a binary number "<<endl;
cin>>binary;
b=binary;
while(binary>0){
r=binary%10;
binary = binary/10;
decimal=decimal+(pow(2,i))*r;
i++;
}
cout<<decimal<<" is decimal value of binary "<<b<<endl;
return 0;
}
25. By Raginikumari15
Made by Raginikumari15. ( Source )
#include<iostream>
using namespace std;
int main() {
int num;
cin>> num;
int tem=num;
int bin = 0; // binary to decimal conversion
int pv = 1;
while(tem>0){
//int pv = 1;
int rem = tem%10;
tem = tem/10;
bin = bin+rem*pv;
//tem = tem/10;
pv= pv *2;
}
cout<< bin;
}
26. By Prashant Chauhan
Made by Prashant Chauhan. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
int bTod(long long);
int main()
{
long long l;
cout << "Enter in binary : ";
cin >> l; cout << l << " in binary = " << bTod(l) << " in decimal";
return 0;
}
int bTod(long long n)
{
int Number = 0, i = 0, r;
while (n!=0)
{
r = n%10; n /= 10;
Number += r*pow(2,i); ++i;
}
return Number;
}
27. By mdminhaziftekhar
Made by mdminhaziftekhar. ( Source )
#include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cout<<"Enter a binary number : ";
getline(cin,str);
int decimal = 0;
int position = str.length() - 1;
for(int i=0; i<str.length(); i++){
decimal += ((str.at(i) - '0') * pow(2,position));
position--;
}
cout<<"Decimal of the given binary is "<<decimal<<endl;
return 0;
}
28. By Abdo essam
Made by Abdo essam. ( Source )
#include <math.h>
#include <iostream>
using namespace std;
bool convert(string &number , double &decimal){
int n = number.length() ;
int power = 0 ;
for (int i = n-1 ; i >= 0 ; i--) {
if (number[i] > '1' || number[i] < '0' ){
cout << "Enter binary numbers !" << endl;
return false ;
}
else{
decimal = decimal + (int)pow(2,power) * ((int)number[i] - '0') ;
power++ ;
}
}
return true ;
}
int main() {
string binary = "" ;
double decimal = 0;
cout << " Enter your number in binary : " ;
cin >> binary ;
if (convert(binary,decimal)){
cout << "Your number in decimal : " << decimal << endl;
}
return 0;
}
29. By Kumari Nikita
Made by Kumari Nikita. C++ program to convert binary to decimal. ( Source )
#include <iostream>
using namespace std;
// Function to convert binary to decimal
int binaryToDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
// Driver program to test above function
int main()
{
int num = 11001100;
cout << binaryToDecimal(num) << endl;
}
30. By ellissentoso
Made by ellissentoso. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
int bin2dec(long long);
int main(int argc, char *argv[])
{
long long n;
cout<<"Enter #: ";
cin>>n;
cout << n << " in binary is "<<bin2dec(n)
<<" in decimal";
return 0;
}
int bin2dec(long long n) {
int dec = 0;
int i = 0;
int remainder = 0;
while (n != 0) {
remainder = n%10;
n /= 10;
dec += remainder*pow(2,i);
++i;
}
return dec;
}
31. By Adesh Choudhar
Made by Adesh Choudhar. ( Source )
#include <iostream>
using namespace std;
int main()
{
int bin,binary,decimal,digit,base;
decimal=0;
base=0;
cout<<"Enter A Binary Number : \n";
cin>>binary;
cout<<"The Binary Number you have entered is : "<<binary<<endl;
bin=binary;
while(binary)
{
digit=binary%10;
decimal+=digit<<base;
base+=1;
binary/=10;
}
cout<<"Decimal Number of entered Binary Number is : "<<decimal;
return 0;
}
32. By Bibaswan Roy
Made by Bibaswan Roy. ( Source )
#include <iostream>
using namespace std;
int main() {
int deci = 0, j, i = 1;
long long int num;
cin >> num; //enter a binary number in positive
if (num < 0){cout << "Please enter a positive expression";}
else{
while (num > 0){
j = num % 10;
deci = deci + (j * i);
i *= 2;
num /= 10;
}
cout << "The required decimal number is "<< deci;}
return 0;
}
33. By Nithin X Johnson
Made by Nithin X Johnson. ( Source )
//c++ code to convert binary to decimal
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,a,rem,x=0,len;
cout<<"Enter the binary number:";
cin>>n;
a=n;
cout<<a<<endl;
if(n!=0)
{
while(n!=0){
n/=10;
x++;}
}
else
x=1;
len=x;
int arr[len];
int y=0;
while (a!=0)
{
rem=a%10;
arr[y]=rem;
a/=10;
y++;
}
int dec=0;
for(int i=0;i<len;i++)
{
dec=arr[i]*pow(2,i) + dec;
}
cout<<"The decimal number is:"<<dec;
return 0;
}
34. By HUGOW04
Made by HUGOW04. ( Source )
#include <iostream>
using namespace std;
int binaryToDecimal(int number)
{
int num = number;
int decimal = 0;
int base = 1;
int temp = num;
while (temp)
{
int last = temp % 10;
temp = temp / 10;
decimal += last * base;
base = base * 2;
}
return decimal;
}
int main()
{
int num = 10101001;
cout << binaryToDecimal(num) << endl;
}
35. By Harshit Nema
Made by Harshit Nema. ( Source )
#include<iostream>
using namespace std;
int binarytodecimal(int n);
int main()
{
int n;
cin >> n;
cout << binarytodecimal(n);
return 0;
}
int binarytodecimal(int n)
{
int ans = 0;
int x = 1;
while(n != 0)
{
int num = n%10;
ans += num*x;
n = n/10;
x *= 2;
}
return ans;
}
36. By Bhawna Saini
Made by Bhawna Saini. ( Source )
#include <iostream>
using namespace std;
int binaryToDecimal (int n){
int ans=0;
int x=1;
while(n>0){
int y = n%10;
ans+=x*y;
x*=2;
n/=10;
}
return ans;
}
int main() {
int n;
cin>>n;
cout<< binaryToDecimal(n)<<endl;
return 0;
}
37. By Md.Sondhi Sheikh
Made by Md.Sondhi Sheikh. ( Source )
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n=0,s=0,binary;
cout<<"Enter the number in binary:";
cin>>binary;
while(binary)
{
s=s+pow(2,n)*(binary%10);
binary=binary/10;
n++;
}
cout<<"decimal number is: "<<s;
return 0;
}
38. By mominarzan
Made by mominarzan. ( Source )
#include <iostream>
using namespace std;
int BtoD(int n)
{
int ans=0,x=1;
while(n>0){
int y=n%10;
ans += x*y;
x *= 2;
n /= 10;
}
return(ans);
}
int main()
{
int n,sum=0;
cout<<"enter the Binary number"<<endl;
cin>>n;
cout<<"Decimal Equivalent is "<<BtoD(n)<<endl; // call of the function binary to Decimal
}
39. By Sushil Jangid
Made by Sushil Jangid. ( Source )
#include <iostream>
#include<math.h>
using namespace std;
int main() {
long int a,b,c=0;
int i=0;
//please binary number
cin>>a;
cout<<"Decimal number of "<<a;
while(a>0)
{
b=a%10;
a=a/10;
c=c+b*pow(2,i);
i++;
}
cout<<" is = "<<c;
return 0;
}
40. By Viktor Bienius
Made by Viktor Bienius. ( Source )
#include <iostream>
#include <math.h>
using namespace std;
float b;
int c=0;
int i=0;
int main()
{
cout<<"Binary: ";
cin>>b;
cout<<b<<"\n";
while(b>1)
{
b=b*0.1;
if (b>floor(b))
{
c+=pow(2,i);
}
i++;
b=floor(b);
}
if (i>0)
{
c+=pow(2,i++);
}
else
{
if (b=0)
{
c=0;
}
else
{
c=1;
}
}
cout<<"Decimal: "<<c;
return 0;
}
41. By Michał Dobranowski
Made by Michał Dobranowski. ( Source )
#include <iostream>
using namespace std;
int main() {
long long bin;
long dec = 0;
cout << "enter a binary number\n";
cin >> bin;
for (long i = 1; i <= 524288; i *= 2) {
dec += bin % 10 * i;
bin /= 10;
}
cout << dec;
}
42. By Suryapardana
Made by Suryapardana. ( Source )
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
//angga gans
// g++ 6.3.0
int main()
{
string bin;
int i,n,sum=0;
cin>>bin;
n=bin.length();
for(i=n-1;i>=0;i--)
{
if(bin[i]=='1')
sum=sum+pow(2,n-i-1);
}
cout<<sum;
}
43. By Mukul
Made by Mukul. ( Source )
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
long int i,num=0,mul2=1;
int r;
cin>>i;
while(i>0)
{
r=i%10;
if(r>1)
{
cout<<"Entered Number is Not Binary"<<endl;
exit(1);
}
num+=r*mul2;
i=i/10;
mul2=mul2*2;
}
cout<<num;
return 0;
}
44. By Eligijus Silkartas
Made by Eligijus Silkartas. ( Source )
#include <iostream>
using namespace std;
bool valid_bin(string);
int main() {
string bin;
cout << "Please enter a binary number.\n";
cout << "Example: 101010\n";
cin >> bin;
if(!valid_bin(bin)) {
cout << "error: " << bin << " is not a binary number!";
return 1;
}
int dec = 0;
int j = 0;
for(int i = bin.length() - 1; i >= 0; i--) {
dec += (bin[i]-48) * (1 << j++);
}
cout << bin << " => " << dec << endl;
return 0;
}
bool valid_bin(string bin) {
for(int i = 0; i < bin.length(); i++) {
if(bin[i] != '0' && bin[i] != '1') {
return false;
}
}
return true;
}
45. By Chris
Made by Chris. The program reads a string containing a binary number from a user input and convert it to an integer. ( Source )
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// All variables we need.
string input;
int output = 0, length;
cin >> input; // Reading the binary number.
length = input.length()-1; // Saving calculation time.
// We'll use a try/catch-block in case the input is invalid.
try
{
// If the input is empty, throw an exception.
if (input == "")
throw "No input";
// We're going to start reading at the rightmost symbol.
for (int i = 0; i <= length; i++)
{
// If the symbol is 1, add 2^i to the decimal output.
if (input[length - i] == '1')
output += pow(2, i);
// If the symbol is neither 1 or 0, throw an exception.
else if (input[length - i] != '0')
throw "Invalid input";
}
// If there was no exception, print the result.
cout << output;
}
// If an exception occurs, stop the calculation and print an error message.
catch (const char *e)
{
cout << e;
}
return 0;
}
46. By Roni Berlin
Made by Roni Berlin. ( Source )
#include <iostream>
using namespace std;
int dec(int bin){
int sum=0;
int temp=1;
while(temp){
if(bin%10==1){
sum+=temp;
}
bin/=10;
temp*=2;
}
return sum;
};
int main() {
int bin;
cin>>bin;
cout<<dec(bin);
return 0;
}
47. By Aman Kumar
Made by Aman Kumar. A simple C++ Binary to Decimal Converter. ( Source )
#include <iostream>
using namespace std;
int binarytodecimal(int n){
int ans=0;
int x=1;
while(n>0){
int y=n%10;
ans+=x*y;
x*=2;
n/=10;
}
return ans;
}
int main() {
int n;
cin>>n;
cout<<binarytodecimal(n);
return 0;
}
48. By ֆɦɨʀօʍɨ
Made by ֆɦɨʀօʍɨ. ( Source )
// Binary Translator_CC.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <cmath>
int main()
{
std::string bin;
double value = 0;
std::cin >> bin;
std::cout << "Binary: " << bin << std::endl;
int len = bin.size();
for (int i = len - 1; i >= 0; i--) {
double indic = ((double)len - (double)i);
double weight = (pow(2, indic - 1));
char pos = bin[i];
if (pos == '1') {
value += weight;
}
else if (pos != '0') {
std::cout << "Character given at position " << (i + 1) << " cannot be parsed as binary input.";
return 0;
}
std::cout << "Index: " << indic << "; Value: " << pos << "; Value: " << weight << "; Power (2^" << indic - 1 << ") = " << value << std::endl;
}
std::cout << "\n" << "Result: " << value;
}