
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
- Related Articles
- C++ Program to Calculate Sum of Natural Numbers
- Program to find sum of harmonic series in C++
- C++ Program for sum of arithmetic series
- C program to find the sum of arithmetic progression series
- C Program for sum of cos(x) series
- Program for sum of geometric series in C
- C program to find sum and difference using pointers in function
- Java Program to Calculate Sum of Two Byte Values Using Type Casting
- C++ Program to Calculate Average of Numbers Using Arrays
- C++ Program to Calculate Power Using Recursion
- C++ Program to calculate the sum of all odd numbers up to N
- How to calculate sum of array elements using pointers in C language?
- 8085 program to find the sum of a series
- Python program to find the sum of sine series
- C++ program to Calculate Factorial of a Number Using Recursion

Advertisements