I like to calculate the following formula using c programming. The formula is as follows:
I have written the c program based on this formula
#include<stdio.h>
#include<math.h>
int main(){
int n,i;
double sum=0;
printf("Enter the value for n");
scanf("%d",&n);
for (i=0; i<=n; i ){
sum = sum 4*(pow(-1,i))/((2*i) 1);
}
printf("sum of the series: %lf",sum);
}
return 0;
I am sharing the result that I have got after run the program and the result I got while calculating the result mathematically.
As you can see from picture 1 if I try to calculate the sum for different value of n like 1, 4,13 the sum was 2.67, 3.339, 3.0702 respectively.
However, when I tried to calculate this formula mathematically I got some different result.
Mathematically I got
n= 1 ans: 3.2
n=4 ans: 0.96825
Can anyone please guide what I am missing in code?
Thank you.
CodePudding user response:
In 2nd iteration of your manual calculation, 2x1 1 = 3 but you're taking it as 5