Write a program in C++ to find the sum of the series 1 +11 + 111 + 1111 + .. n terms using the single inheritance.
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 |
#include <iostream> using namespace std; class A { public: int n; }; class b:public A { public: int um() { cin>>n; int s=0; int t=1,i=1; for(i=1;i<=n;i++) { cout<<" "<<t<<" "; if (i<n) { cout<<"+"; } s=s+t; t=(t*10)+1; } cout<<endl<<"The Sum is :"<<s; } }; int main() { b F; F.um(); } |
WAP in C++ to find the sum of the series 1 +11 + 111 + 1111 + .. n terms with the help of multiple inheritances.
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 |
#include <iostream> using namespace std; class A { public: int n; }; class b { public: int s; }; class show:public A,public b { public: int um() { s=0; cin>>n; int t=1,i=1; for(i=1;i<=n;i++) { cout<<" "<<t<<" "; if (i<n) { cout<<"+"; } s=s+t; t=(t*10)+1; } cout<<endl<<"The Sum is :"<<s; } }; int main() { show F; F.um(); } |
Write a program in C++ to show the sum of the series 1 +11 + 111 + 1111 and n terms using the multi level inheritance.
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 |
#include <iostream> using namespace std; class A { public: int n; }; class b:public A { public: int s=0; }; class c:public b { public: int um() { cin>>n; int t=1,i=1; for(i=1;i<=n;i++) { cout<<" "<<t<<" "; if (i<n) { cout<<"+"; } s=s+t; t=(t*10)+1; } cout<<endl<<"The Sum is :"<<s; } }; int main() { c F; F.um(); } |