BINARY TO DECIMAL CPP SOURCE CODE

/*DECIMAL.CPP*/
#include "calculator.h"
int binary_decimal(int n);
int decimal_binary(int n);
#include <iostream>
using namespace std;
void ScientificCalculator::decimal()
{
   int n;
   char c;
   cout << "Instructions: " << endl;
   cout << "1. Enter alphabet 'd' to convert binary to decimal." << endl;
   cout << "2. Enter alphabet 'b' to convert decimal to binary." << endl;
   cin >> c;
   if (c =='d' || c == 'D')
   {
       cout << "Enter a binary number: ";
       cin >> n;
       cout << n << " in binary = " << binary_decimal(n) << " in decimal";
   }
   if (c =='b' || c == 'B')
   {
       cout << "Enter a binary number: ";
       cin >> n;
       cout << n << " in decimal = " << decimal_binary(n) << " in binary";
   }
}
int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{
    int rem, i=1, binary=0;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
}
int binary_decimal(int n) /* Function to convert binary to decimal.*/
{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*(2^i);
        ++i;
    }
    return decimal;
}

Comments

Popular posts from this blog

Scientific Calculator Project Proposal