Write a C++ program to find the number and sum of all integer between 100 and 200 which are divisible by 9 with constructor destructor.
C++ sum of all integer divisible by a number with constructor 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 |
//Write a program to find the number and T4Tutorials_Sum of all integer between 100 and 200 which are divisible by 9. //constructor & destructor #include<iostream> using namespace std; class number { public: number(int i,int T4Tutorials_Sum) { cout<<"numbers that are divisible by 9 between 100 and 200\n"; for(i=101; i<200; i++) if(i%9==0) { cout<<"numbers divisble by 9 is= "<<i<<endl; T4Tutorials_Sum=T4Tutorials_Sum+i; } cout<<"T4Tutorials_Sum of all numbers divisible by 9 is= "<<T4Tutorials_Sum<<endl; } ~number() { cout<<"\n destructor completed its work to free up memory"<<endl; } }; int main() { number f(101,0); } |
Output
C++ constructor Overloading program of sum of all integer divisible by a number with
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 |
//Write a program to find the number and T4Tutorials_Sum of all integer between 100 and 200 which are divisible by 9. //constructor overloaded #include<iostream> using namespace std; class number { private: int i,T4Tutorials_Sum=0; public: number(int n) { for(i=n; i<200; i++) if(i%9==0) { cout<<"numbers divisble by 9 is= "<<i<<endl; T4Tutorials_Sum=T4Tutorials_Sum+i; } cout<<" Sum of all numbers divisible by 9 is= "<<T4Tutorials_Sum<<endl; } number(long int a) { int i,T4Tutorials_Sum=0; for(i=a; i<200; i++) if(i%9==0) { cout<<"numbers divisble by 9 is= "<<i<<endl; T4Tutorials_Sum=T4Tutorials_Sum+i; } cout<<"T4Tutorials_Sum of all numbers divisible by 9 is= "<<T4Tutorials_Sum<<endl; } }; int main() { int choice; cout<<"enter choice"; cin>>choice; switch(choice) { case 0: { int n; cout<<"enter first number to find the all numbers that are divisible by 9 between 100 and 200\n"; cin>>n; number obj(n); break; } case1: { long int a; cout<<"enter first number to find the all numbers that are divisible by 9 between 100 and 200\n"; cin>>a; number obj2(a); break; } default: cout<<"invalid input"; } } |