Multiple inheritances C++ to display the n terms of odd natural number and their sum
Write a program in C++ to display the n terms of odd natural number and their sum like:
1 3 5 7 … n using 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 |
#include<iostream> using namespace std; class even { public: int n; }; class B { public: int a; }; class s:public even,public B { public: int sum() { cout<<"enter the value : To print Even Number"<<endl; cin>>n; a=0; for(int
i=1;i<=n;i++) { cout<<2*i-1<<" "; a=a+(2*i-1); cout<<endl; } cout<<"sum of given input = "<<a; } }; int main() { s um; um.sum(); } |