Write a C++ program to find Strong Numbers within a range of numbers by using constructor overloading in C++.
We are using more than one constructor of the class T4Tutorials_Strong_Numbers
with the same name, and it is called constructor overloading.
In this program, the constructor must obey one or both of the following rules.
- All constructors with the same name and having a different number of parameters.
T4Tutorials_Strong_Numbers()
 and another constructor asÂTT4Tutorials_Strong_Numbers(int one, int two)
.
- All constructors with the same name and have the same number of parameters but of different data types.
T4Tutorials_Strong_Numbers(int one, int two)
and another constructor asT4Tutorials_Strong_Numbers(double one, double two)
.
Note: In this example, we are overloading the constructor with the following rule;
T4Tutorials_Strong_Numbers()
and another constructor as T4Tutorials_Strong_Numbers(int one, int two)
.
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#include<iostream> using namespace std; class T4Tutorials_Strong_Numbers { protected : int i, n, n1; int s1 , j, k; int en, sn; public : T4Tutorials_Strong_Numbers() { s1 = 0; long T4Tutorials_factorial; cout<<"Input starting range of numbers."<<endl; cin>>sn; cout<<"Input ending range of numbers."<<endl; cin>>en; cout << "The Strong numbers are."<<endl; for (k = sn; k <= en; k++) { n1=k; s1 = 0; for (j = k; j > 0; j = j / 10) { T4Tutorials_factorial = 1; for (i = 1; i <= j % 10; i++) { T4Tutorials_factorial = T4Tutorials_factorial * i; } s1 = s1 + T4Tutorials_factorial; } if (s1 == n1) { cout << n1 << " "; } } cout<<endl; } T4Tutorials_Strong_Numbers(int one, int two) { int long T4Tutorials_factorial=0; cout << "The Strong numbers are."<<endl; for (k = one; k <= two; k++) { n1=k; s1 = 0; for (j = k; j > 0; j = j / 10) { T4Tutorials_factorial = 1; for (i = 1; i <= j % 10; i++) { T4Tutorials_factorial = T4Tutorials_factorial * i; } s1 = s1 + T4Tutorials_factorial; } if (s1 == n1) { cout << n1 << " "; } } cout<<endl; } }; int main() { abc: int option; cout<<"Enter 1 for T4Tutorials_Strong_Numbersctor with No Parameter."<<endl; cout<<"Enter 2 for T4Tutorials_Strong_Numbersuctor with Parameter."<<endl; cout<<"Enter 1 or 2 here. "; cin>>option; system("cls"); if(option==1) { cout<<"You Have Slected No Paramter consuctor.... "; T4Tutorials_Strong_Numbers a; } else if(option==2) { cout<<"You Have Selected a constructor with the Parameters."<<endl;; int one,two; cout<<"Enter the Starting range here."<<endl;; cin>>one; cout<<"Enter Ending range : "<<endl; cin>>two; T4Tutorials_Strong_Numbers(one,two); } else { cout<<"Your Input is Wrong : Try Again."<<endl; goto abc; } } |
Output
Enter 1 for T4Tutorials_Strong_Numbersctor with No Parameter.
Enter 2 for T4Tutorials_Strong_Numbersuctor with Parameter.
2
You Have Selected a constructor with the Parameters.
Enter the Starting range here.
1
Enter Ending range :
20
The Strong numbers are.
1Â 2