skip to main |
skip to sidebar
#include<iostream.h>
#include<math.h>
#include<conio.h>
class number
{
long int dec,i,d,n,j;
long bin,num,b[50];
public:
void bin_to_dec();
void dec_to_bin();
};
void number::bin_to_dec()
{
dec=0;
i=0;
cout<<"enter the binary no\n";
cin>>num;
bin=num;
for(i=0;bin>0;i++)
{
d=bin%10;
dec=dec+(d*pow(2,i));
bin=bin/10;
}
cout<<"\ndecimal equivalent of"<<num<<"is:"<<dec;
}
void number::dec_to_bin()
{
cout<<"enter the decimal number\n";
cin>>n;
num=n;
for(i=0;num>0;i++)
{
b[i]=num%2;
num=num/2;
}
cout<<"the binary number equivalent to"<<n<<"is";
for(i=i-1;i>=0;i--)
{
cout<<b[i];
}}
void main()
{
int ch;
clrscr();
number ob;
do
{
cout<<"\nmenu\n1-binary to decimal\n2-decimal to binary\n3-exit\n";
cin>>ch;
switch(ch)
{
case 1:
ob.bin_to_dec();
break;
case 2:
ob.dec_to_bin();
break;
}}
while(ch<3);
getch();
}
OUTPUT:
menu
1-binary to decimal
2-decimal to binary
3-exit
1e
Enter the binary no
1010
decimal equivalent of 1010 is 10
menu
1-binary to decimal
2-decimal to binary
3-exit
2e
Enter the decimal number
7
the binary number equivalent to 7 is 111
menu
1-binary to decimal
2-decimal to binary
3-exit
3
0 comments:
Post a Comment