Write a C++ program to find Strong Numbers within a range of numbers using constructor C++.
The constructor T4Tutorials_Strong_Numbers() is a member function of the class T4Tutorials_Strong_Numbers. The constructor T4Tutorials_Strong_Numbers() has the same name as the name of its class T4Tutorials_Strong_Numbers.
- When a new object
aof the classT4Tutorials_Strong_Numbersis executed, the constructorT4Tutorials_Strong_Numbers()also executed automatically. - The constructor
T4Tutorials_Strong_Numbers()has no data type. Even we can’t use void also. - The constructor
T4Tutorials_Strong_Numbers()can have arguments. - The constructor
T4Tutorials_Strong_Numbers()can be only public. - There is no inheritance of the constructor
T4Tutorials_Sum().
|
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 |
#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<<"Please input the starting range of numbers."<<endl; cin>>sn; cout<<"Please input the 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; } }; int main() { int option; cout<<"You Have Selected constructor with No Parameters."<<endl; T4Tutorials_Strong_Numbers a; cin>>option; } |
Output
You Have Selected constructor with No Parameters.
Please input the starting range of number :
1
Please input the ending range of numbers.
145
The Strong numbers are.
1 2 145
