3. Program that displays the sum of the following series using do-while loop. 1 + 1/4 + 1/8 + ... + 1/100

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 3:

Write a program that displays the sum of the following series using do-while loop.

1 + 1/4 + 1/8 + ... + 1/100

Solution:

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    float n=4,sum=1;
    do
    {
        sum=sum+1/n;
        n+=4;
    }
    while(n<=100);
    cout<<"The sum of the series is:"<<setprecision(4)<<sum<<endl;
    return 0;
}

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

Previous Post:
2. Program to display number and their sum using while loop.

Comments