Series Program in C++

This post contain different series program in c++ language with there code and output. These code are implement using the class in the c++ language.

WAP in C++ To generate a series i.e. 1-x+x^2/2!-x^3/3!+……x^n/n!

The program code for above question in C++ language is as follows:

//To generate a series i.e. 1-x+x^2/2!-x^3/3!+......x^n/n!//
#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:float sum,term;
	int i,j,k,fact,sign;
public:series()
       {
       sum=1.0;
       term=0.0;
       i=j=0;
       fact=1;
       sign=-1;
       }
       double generate(int x,int n);
       ~series()
       {
       }
};
double series::generate(int x,int n)
{
cout<<"1   ";
for(i=1;i<=n;i++)
{
fact=1;
for(j=1;j<=i;j++)
{
fact*=j;
}
term=pow(x,i)/fact*sign;
cout<<term<<"   ";
sum+=term;
sign*=-1;
}
return(sum);
}
int main()
{
series s1;
double x,n;
cout<<"enter limit & value of variable:";
cin>>n>>x;
float result=s1.generate(x,n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

WAP in C++ To generate a series 1-x^2/3!+x^4/5!-…….x^n/(n+1)!

The program code for above question in C++ language is as follows:

// To generate a series 1-x^2/3!+x^4/5!-.......x^n/(n+1)!//

#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:float sum,term;
	int i,j,k,fact,sign;
public:series()
       {
       sum=1.0;
       term=0.0;
       i=j=0;
       fact=1;
       sign=-1;
       }
       double generate(int x,int n);
       ~series()
       {
       }
};
double series::generate(int x,int n)
{
cout<<"1   ";
for(i=2;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i+1;j++)
{
fact*=j;
}
term=pow(x,i)*sign/fact;
cout<<term<<"   ";
sum+=term;
sign*=-1;
}
return(sum);
}
int main()
{
series s1;
double x,n;
cout<<"enter limit & value of variable:";
cin>>n>>x;
double result=s1.generate(x,n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

Series

WAP in C++ To generate a series 1+x/1!+x^3/2!+……x^(2n-1)/n!

The program code for above question in C++ language is as follows:

//To generate a series 1+x/1!+x^3/2!+......x^(2n-1)/n!\\

#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:float sum,term;
	int i,j,k,fact;
public:series()
       {
       sum=1.0;
       term=0.0;
       i=j=0;
       fact=1;
       }
       double generate(int x,int n);
       ~series()
       {
       }
};
double series::generate(int x,int n)
{
int k=1;
cout<<"1   ";
for(i=1;i<=(2*n-1);i+=2)
{
fact=1;
for(j=1;j<=k;j++)
{
fact*=j;
}
term=pow(x,i)/fact;
cout<<term<<"   ";
sum+=term;
k++;
}
return(sum);
}
int main()
{
series s1;
double x,n;
cout<<"enter limit & valur of variable:";
cin>>n>>x;
float result=s1.generate(x,n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

Series

WAP in C++ To generate a series i.e. (2^2)+(2^2+4^2)+(2^2+4^2+6^2)……

The program code for above question in C++ language is as follows:

//To generate a series i.e. (2^2)+(2^2+4^2)+(2^2+4^2+6^2)......//
#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:int s,sum,term,i,j;
public:series()
       {
       s=sum=0;
       term=0;
       i=j=0;
       }
       double generate(int n);
       ~series()
       {
       }
};
double series::generate(int n)
{
for(i=2;i<=n;i+=2)
{
s=0;
for(j=2;j<=i;j+=2)
{
s+=pow(j,2);
}
term=s;
cout<<term<<"   ";
sum+=term;
}
return(sum);
}
int main( )
{
series s1;
int n;
cout<<"enter limit of series:";
cin>>n;
int result=s1.generate(n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

Series

WAP in C++ To generate a series i.e. 1+(1+2)/2+(1+2+3)/3……

The program code for above question in C++ language is as follows:

//To generate a series i.e. 1+(1+2)/2+(1+2+3)/3......//
#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:float sum,term,s,i,j;
public:series()
       {
       sum=term=0.0;
       i=s=j=0;
       }
       double generate(int n);
       ~series()
       {
       }
};
double series::generate(int n)
{
for(i=1;i<=n;i++)
{
s=0;
for(j=1;j<=i;j++)
{
s+=j;
}
term=s/i;
cout<<term<<"   ";
sum+=term;
}
return(sum);
}
int main()
{
series s1;
int n;
cout<<"enter limit of series:";
cin>>n;
float result=s1.generate(n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

WAP in C++ To generate a series i.e. (2^2)+(2+4)^2+(2+4+6)^2……

The program code for above question in C++ language is as follows:

//To generate a series i.e. (2^2)+(2+4)^2+(2+4+6)^2......//
#include<iostream>
#include<math.h>

using namespace std;
class series
{
private:int s,sum,term,i,j;
public:series()
       {
       s=sum=0;
       term=0;
       i=j=0;
       }
       double generate(int n);
       ~series()
       {
       }
};
double series::generate(int n)
{
for(i=2;i<=n;i+=2)
{
s=0;
for(j=2;j<=i;j+=2)
{
s+=j;
}
term=pow(s,2);
cout<<term<<"   ";
sum+=term;
}
return(sum);
}
int main()
{
series s1;
int n;
cout<<"enter limit of series:";
cin>>n;
int result=s1.generate(n);
cout<<"\nsum of above series:"<<result<<endl;
return 0;
}

The output of following code is as follows:

Series

Discover more from easytechnotes

Subscribe to get the latest posts sent to your email.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top