31. Program to add first nine terms of following series using for and while loop: 1/3! + 5/4! + 9/5! + ... where ! indicates factorial.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 31:

Write a program to add first nine terms of following series using for and while loop:
1/3! + 5/4! + 9/5! + ... where ! indicates factorial.

Solution:

Using a for loop:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int c;
    float den, f, sum=0, num=1;
    for(den=3;den<=11;den++)
    {
        f=1;
        for(c=1;c<=den;c++)
        {
            f=f*c;
        }
        sum = sum+(num/f);
        num+=4;
    }
    cout<<"The sum of the series is: "<<sum<<endl;
    getch();
}

Using a while loop:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int c;
    float den=3, f, sum=0, num=1;
    while(den<=11)
    {
        f=1;
        c=1;
        while(c<=den)
        {
            f=f*c;
            c++;
        }
        sum = sum+(num/f);
        num+=4;
        den++;
    }
    cout<<"The sum of the series is: "<<sum<<endl;
    getch();
}


Let me know in the comment section if you have any question.

Previous Post:
30. Program that will calculate the sum of every third integer, beginning with i=2 (i.e., calculate the sum 2 + 5+ 8 + 11 + ...) for all values of i that are less than 100.

Comments