برنامه تبدیل عدد از مبنتای دو 2 به ده 10 باینری به دسیمال(binary to decimal) به زبان سی پلاس پلاس
جمعه, ۱۶ مهر ۱۳۹۵، ۰۵:۲۵ ق.ظ
gameover.blog.ir
برنامه زیر عدد باینتری رو به دهدهی میبره و بلعکس
/* C++ programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */
#include <iostream>
#include <cmath>
using namespace std;
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
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";
}
return 0;
}
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*pow(2,i);
++i;
}
return decimal;
}