C++ Program : Sum of the Series using Constructor

 // 11 C++ program - Find the sum of the series 1 + x + x2+ x3 +….xn using constructors.

// Write a C++ program to find sum of the series 1 + x + x2 + x3 + ….xn using constructors.


#include<iostream.h>

#include<conio.h>

#include<math.h>


class Series

{

private:

int sum, x, n;

public:

Series(int y, int m) // Parameterized Constructor

{

sum = 1;

x = y;

n = m;

for(int i=1;i<=n;i++)

sum=sum+pow(x,i);

}

float getSum();

};


float Series::getSum()

{

return sum;

}


void main()

{

int x,n;


clrscr( );

cout<<"Enter the value for Base(X)="<<endl;

cin>>x;


cout<<"Enter the value for Power(n)"<<endl;

cin>>n;


Series s1(x,n); // Calling Parameterized Constructor

Series s2 = s1; // Copy Constructor


cout<<"Sum of Series using Parameterised Constructor:";

cout<<s1.getSum()<<endl;


cout<<"Sum of Series using Copy Constructor:";

cout<<s2.getSum( );

getch( );

}

Comments

Popular posts from this blog

Karnataka I PUC Computer Science 2024 Study Material | SECOND PUC HANDBOOK EXAM 2025

PYTHON: Tips and Tricks