C++ Program to convert a decimal number into binary with constructor overloading.
Write a program in C++ to convert a decimal number into binary without using an array by using the constructor overloading.
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include<iostream> using namespace std; class T4Tutorials_Decimal_Constructor { protected : int i,j; int decimal, binary; int decimal1, binary1; int decimal2, binary2; public : T4Tutorials_Decimal_Constructor(int n) { decimal=n; i=1;j=n,binary=0; for(j=n;j>0;j=j/2) { binary=binary+(n%2)*i; i=i*10; n=n/2; } cout<<"binary number = "<<binary<<endl; } T4Tutorials_Decimal_Constructor(int n1,int n2) { decimal1=n1; decimal2=n2; i=1; for(j=n1;j>0;j=j/2) { binary1=binary1+(n1%2)*i; i=i*10; n1=n1/2; } cout<<"binary number = "<<binary1<<endl; i= 1; for(j=n2;j>0;j=j/2) { binary2=binary2+(n2%2)*i; i=i*10; n2=n2/2; } cout<<"binary number = "<<binary2<<endl; } }; int main() { int option; cout<<"Enter 1 FOR Single parameter Constructor"<<endl; cout<<"Enter 2 FOR Multiple Paramter construcor "<<endl; cin>>option; if(option ==1) { cout<<"You Have Slected Single Paramater Constructor."<<endl<<endl; int n; cout<<"Enter a Decimal Number to convert into BINARY Number : "<<endl; cin>>n; T4Tutorials_Decimal_Constructor a(n); } else if(option==2) { cout<<"You Have slected Multiple Paramater Constructor."<<endl<<endl; int n1,n2; cout<<"Enter First Decimal no. to convert into BINARY no. : "; cin>>n1; cout<<"Enter Second Decimal no. to convert into BINARY no. : "; cin>>n2; T4Tutorials_Decimal_Constructor a(n1, n2); } else cout<<"invalid Input "; } |
Output
Enter 1 FOR Single parameter Constructor.
Enter 1 FOR Single parameter Constructor.
2
You Have Selected Single Parameter Constructor.
Enter First Decimal no. to convert into BINARY no: 4
Enter Second Decimal no. to convert into BINARY no: 8
binary number=100
binary number=1000