Sample Qustion 7

Write a C++ program to convert given binary number into a decimal number.

#include <iostream>
using namespace std;
int binaryToDecimal(int n)
{
    int num = n;
    int dec_value = 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;
}
int main()
{

    int num;
    cout << "enter number : " << endl;
    cin >> num;
    cout << binaryToDecimal(num) << endl;
    
}



download file  cpp file


do you have any qustion please comment



click the advertisement to help for me ....




Comments

Popular posts from this blog

Java Question Data structure 04

Sample Qustion 1