Cyberark Academy Programming Codes. Example 1

78 2 0
                                    

Cyberark Academy Programming Codes

Example 1: C++ Program to convert binary number to decimal
#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;
}
Output

Enter a binary number: 1111
1111 in binary = 15

#BinaryToDecimal
#C++Programming
#CyberarkAcademy

CSS NC IIWhere stories live. Discover now