Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Calculate the sum of the series [ 1-X^2/2!+X^4/4!- .........]

C For Loop: Exercise-18 with Solution

Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].

Sample Solution:

C Code:

#include <stdio.h>
void main()
{
	float x,sum,t,d;
	int i,n;
	printf("Input the Value of x :");
	scanf("%f",&x);
	printf("Input the number of terms : ");
	scanf("%d",&n);
	sum =1; t = 1;
	for (i=1;i<n;i++)
	{
	  d = (2*i)*(2*i-1);
	  t = -t*x*x/d;
	  sum =sum+ t;
	}
	printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",sum,n,x);
} 

Flowchart:

Flowchart: Calculate the sum of the series 1-X^2/2!+X^4/4!- .

or

#include <stdio.h>

void main()
{
	float x,s,t,num=1.00,fac=1.00;
	int i,n,pr,y=2,m=1;

	printf("Input the Value of x :");
	scanf("%f",&x);
	printf("Input the number of terms : ");
	scanf("%d",&n);
	s=1.00; t=1.00;

	for (i=1;i<n;i++)
	{
            for(pr=1;pr<=y;pr++)
                 {
                   fac=fac*pr;
                   num=num*x;

                 }   
          m=m*(-1);
          num=num*m;
          t=num/fac;
	  s=s+t;
          y=y+2;
          num=1.00; 
          fac=1.00;
          
	}
	printf("\nthe sum = %f\nNumber of terms = %d\nvalue of x = %f\n",s,n,x);
} 

Sample Output:

Input the Value of x :2                                                                                       
Input the number of terms : 5                                                                                 
                                                                                                              
the sum = -0.415873                                                                                           
Number of terms = 5                                                                                           
value of x = 2.000000  

Flowchart:

Flowchart: Calculate the sum of the series 1-X^2/2!+X^4/4!- .

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to make such a pattern like a pyramid with a number which will repeat the number in the same row.
Next: Write a program in C to display the n terms of harmonic series and their sum.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP