series computing using loops

Pages: 12
Hey guys!!
First of all thank you so much for being so patient and helpful with me as I am a complete dummy here.
Secondly Yesss!! I could finally sort my program and solve it all thanks to everyone here.
this was my final code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;
int main()
{

double x, sum=0.0, term;
int n, i;

cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;

//a setting the variables
   term=x;
   sum=term;

//b work for odd numbers only
if(n % 2 != 0 )

//c loop to run for computation

       {
           for ( i=1; i<n; i+=2)

           term*=-x*x/ (( i+1 ) * ( i + 2 ) ) ; //d series computation
           sum += term;

       }


cout<<"y= ="<< sum <<endl;

return 0;
}
Hi @sairaashraf17

Just one final pair of braces { } ....

1
2
3
4
5
6
7
      {
           for ( i=1; i<n; i+=2)
           {          // <===== HERE **********
                term*=-x*x/ (( i+1 ) * ( i + 2 ) ) ; //d series computation
                sum += term;
           }          // <===== AND HERE **********
       }
Topic archived. No new replies allowed.
Pages: 12