# C program to calculate sum of series using predefined function

## Problem

The program to calculate the sum of the following expression

Sum=1-n^2/2!+n^4/4!-n^6/6!+n^8/8!-n^10/10!

User has to enter the value of n at runtime to calculate the sum of the series by using the predefined function power present in math.h library function.

## Solution

It is explained below how to calculate sum of series using predefined function.

## Algorithm

Refer an algorithm given below to calculate sum of series by using the predefined function.

Step 1 − Read the num value

Step 2 − Initialize fact = 1, sum = 1 and n =5

Step 3 − for i= 1 to n

   a. compute fact= fact*i
b. if i %2 = 0
c. then if i=2 or i=10 or i=6
d. then sum+= -pow(num,i)/fact
e. else sum+=pow(num,i)/fact
4. print sum

## Example

Following is the C program to calculate sum of series by using the predefined function

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
int i,n=5,num;
long int fact=1;
float sum=1;
printf("Enter the n value:");
scanf("%d", &num);
for(i=1;i<=n;i++){
fact=fact*i;
if(i%2==0){
if(i==2|i==10|i==6)
sum+= -pow(num,i)/fact;
else
sum+=pow(num,i)/fact;
}
}
printf("sum is %f", sum);
}

## Output

When the above program is executed, it produces the following result −

Enter the n value:10
sum is 367.666656