Tech Study

Write C++ program to convert binary number to decimal

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
#include <math.h>
using namespace std;
 
//Function declartion
int convertBinaryToDecimal(long long n);
 
int main()
{
    long long n;
    cout<<"Enter a binary number: ";
    // Inputting number from user
    cin>>n;
    //Printing binary number to decimal
    cout<<n<<" in binary = "<<convertBinaryToDecimal(n)<<" in decimal";
    return 0;
}
 
//Function to convert binary number to decimal
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;
}
 

Result

Write C++ program to convert binary number to decimal
Write C++ program to convert binary number to decimal

TaggedWrite C Program to convert binary number to decimal

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more