Site icon T4Tutorials.com

Single inheritance 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 single inheritance.

#include<iostream>
using namespace std;
class even
{
	public:
		int n;
};
class A:public even
{
	public:
		int sum()
		{
			int s=0;
			cout<<"enter the value : To print Even Number"<<endl;
			cin>>n;
			for(int i=1;i<=n;i++)
			{
				cout<<2*i-1<<" ";
				s=s+(2*i-1);
				cout<<endl;
			}
			cout<<"sum of given input  = "<<s;
		}
};
int main()
{
	A um;
	um.sum();
}

 

Exit mobile version