Wednesday, 18 June 2014

C++ program to find the sum of series

Here are five C++ program to find sum of different series. 


C++ program to find the sum of series: 1 + (1 + 2) +..+ (1 + 2 +..+ N)
#include<iostream>
using namespace std;
int main()
{
    int temp=0,n;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int y=1;y<=i;y++)
        {
            temp+=y;
        }
    }
    cout<<"Series: 1 + (1 + 2) +..+ (1 + 2 +..+ N) = "<<temp;
    return 0;
}

OUTPUT




C++ program to evaluate the series 1 + 1/2 + 1/3 + .. + 1/N
#include<iostream>
using namespace std;
int main()
{
    float temp=0,n;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        temp+=1/(1.0*i);
    }
    cout<<"Series: 1 + 1/2 + 1/3 + .. + 1/N = "<<temp;
    return 0;
}

OUTPUT




C++ program to evaluate the series 1 + 1/3 + 1/5 + ...+1/x
#include<iostream>
using namespace std;
int main()
{
    float temp=0,n;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        if(i%2==1)
        temp+=1/(1.0*i);
    }
    cout<<"Series: 1 + 1/3 + 1/5 + ...+1/x = "<<temp;
    return 0;
}

OUTPUT




C++ program to evaluate the series 1 - 1/2 + 1/3 -1/4 + ...+1/x
#include<iostream>
using namespace std;
int main()
{
    float temp=0,n,y=1;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        temp+=y*(1/(1.0*i));
        y=-y;
    }
    cout<<"Series: 1 - 1/2 + 1/3 -1/4 + ...+1/x = "<<temp;
    return 0;
}

OUTPUT




C++ program to evaluate the series 1/1! + 2/2! + 3/3! +...+n/n!
#include<iostream>
using namespace std;
long factorial(int n)
{
    long fact=1;
    while(n>0)
    {
        fact*=n;
        n--;
    }
    return fact;
}
int main()
{
    float temp=0,n;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
        temp+=i/(1.0*factorial(i));
    cout<<"Series: 1 + 2/2! + 3/3!+ ...+n/n! = "<<temp;
    return 0;
}

OUTPUT




C++ program to evaluate the series 1 + 2*1 + 3*2 + 4*3 + 5*4 + ... N*N-1
#include<iostream>
using namespace std;
int main()
{
    float temp=1,n;
    cout<<"Enter the value of n ";
    cin>>n;
    for(int i=1;i<=n;i++)
        temp+=i*(i-1);
    cout<<"Series: 1 + 2*1 + 3*2 + 4*3 + 5*4 + ... N*N-1 = "<<temp;
    return 0;
}

OUTPUT




No comments:

Post a Comment