C++ Program to convert Binary
number to Decimal number or vice versa.This program converts either binary number entered by user to decimal
number or decimal number entered by user to binary number in accordance
with the character entered by user.This program asks user to enter alphabet 'b' to convert
decimal number to binary
and alphabet 'd' to convert binary number to decimal.
In accordance with the character entered, user is asked to enter either binary
value to convert to decimal or decimal value to convert to
binary.To perform conversion, two functions are made
Source Code:-
#include <iostream.h>
#include <conio.h>
#include <math.h>
int binary
_decimal(int n);
int decimal_binary(int n);
int main()
{ clrscr();
int n;
char c;
cout<<"Convet Decimal to Binary & vice versa by-Tarun Rawat\n\n";
cout<<"1. Enter alphabet 'd' to convert binary to decimal.\n";
cout<<"2. Enter alphabet 'b' to convert decimal to binary.\n";
cin>>c;
if (c =='d' || c == 'D')
{ cout<<"\nEnter a binary number : ";
cin>>n;
cout<<n<<" in binary = "<<binary_decimal(n)<<" in decimal";
}
if (c =='b' || c == 'B')
{ cout<<"\nEnter a decimal number : ";
cin>>n;
cout<<n<<" in decimal = "<<decimal_binary(n)<<" in binary";
}
getch();
}
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;
}

decimal number to binary

In accordance with the character entered, user is asked to enter either binary

decimal_binary
()
; to convert decimal to binary and binary
_decimal()
; to convert binary to decimal. Decimal number entered by user is passed to decimal_binary()
and this function computes the binary value of that number and returns it main()
function. Similarly, binary number is passed to function binary_decimal()
and this function computes decimal value of that number and returns it to main()
function.Source Code:-
#include <iostream.h>
#include <conio.h>
#include <math.h>
int binary

int decimal_binary(int n);
int main()
{ clrscr();
int n;
char c;
cout<<"Convet Decimal to Binary & vice versa by-Tarun Rawat\n\n";
cout<<"1. Enter alphabet 'd' to convert binary to decimal.\n";
cout<<"2. Enter alphabet 'b' to convert decimal to binary.\n";
cin>>c;
if (c =='d' || c == 'D')
{ cout<<"\nEnter a binary number : ";
cin>>n;
cout<<n<<" in binary = "<<binary_decimal(n)<<" in decimal";
}
if (c =='b' || c == 'B')
{ cout<<"\nEnter a decimal number : ";
cin>>n;
cout<<n<<" in decimal = "<<decimal_binary(n)<<" in binary";
}
getch();
}
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;
}
No comments:
Post a Comment