Constructor C++ to convert an octal number into binary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include<iostream> using namespace std; class binary { private: long int octalnum,p=1; long int dec=0,i=1,j,d; long int binno=0; public: binary() { cout<<"Enter an octal number to convert: "; cin>>octalnum; } int cal() { for(j=octalnum;j>0;j=j/10) { d=j%10; if(i==1) { p=p*1; } else { p=p*8; } dec=dec+(d*p); i++; } i=1; for(j=dec;j>0;j=j/2) { binno=binno+(dec%2)*i; i=i*10; dec=dec/2; } cout<<"Octal number "<<octalnum<<" equivalent to Binary "<<binno; } }; int main() { binary b; b.cal(); } |
Output
Enter an octal number to convert: 8
Octal number 8 is equivalent to Binary: 1000
Destructor C++ to convert an octal number into binary
Write a program in C++ to convert an octal number into binary using Destructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include<iostream> using namespace std; class binary { private: long int octalnum,p=1; long int dec=0,i=1,j,d; long int binno=0; public: binary() { cout<<"Enter an octal number to convert: "; cin>>octalnum; } ~ binary() { for(j=octalnum;j>0;j=j/10) { d=j%10; if(i==1) { p=p*1; } else { p=p*8; } dec=dec+(d*p); i++; } i=1; for(j=dec;j>0;j=j/2) { binno=binno+(dec%2)*i; i=i*10; dec=dec/2; } cout<<"Octal number "<<octalnum<<" equalant to Binary "<<binno; } }; int main() { binary b; } |
Output
Enter an octal number to convert: 12
Octal number 12 is equivalent to Binary: 10000