-->





find sum of series c++ code

Find sum of given series in which user enter a number and program calculate series using while loop.
Series example:
if input number = 100
1/1 +1/2 +1/4 +.......+ 1/100


//this program will calculate sum of following series
// 1/1 +1/2 +1/4 +.......+ 1/100.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    float n=1.0;
    float d=2.0;
    float sum=1.0;
    float temp=0.0;
    cout<<"Enter a number:";
    int number;
    cin>>number;
    while(d<=number)                    //loop will start from denominator=2 & steps 1,2 &3 continues till d=100.
    {
      temp=n/d;                     // 1) temp=1/2=0.5
      sum=sum+temp;                 // 2) sum=1+0.5=1.5
      d=d+2;                        // 3) d=2+2=4
      }
      cout<<"sum of series is:"<<sum<<endl;
      getch();
      return 0;

}

Output:
find sum of series c++ code

It is recommended to dry run the code on paper and changes the series to understand more.
Find more example here: c++ simple examples source codes

1 comment:

  1. I need to learn about how to intialize a paragraph in char type array in c++

    ReplyDelete