C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programs

Sum of series programs/examples in C programming language

Sum of Series Programs in C - This section contains programs to solve (get sum) of different mathematic series using C programming.

Solved series are:

  1. 1+2+3+4+..N<
  2. 1^2+2^2+3^2+4^2+..N^2
  3. 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
  4. 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
  5. 1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms

Sum of Series Programs / Examples using C

1) C program to find sum of all natural numbers.
Series: 1+2+3+4+..N

/*This program will print the sum of all natural numbers from 1 to N.*/

#include<stdio.h>
    
int main()
{
	int i,N,sum;
	
	/*read value of N*/
	printf("Enter the value of N: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
		sum= sum+ i;
	
	/*print the sum*/
	
	printf("Sum of the series is: %d\n",sum);
	
	return 0;
}

    Enter the value of N: 100
    Sum of the series is: 5050



2) C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2

/*This program will print the sum of the square of all natural numbers from 1 to N.*/

#include<stdio.h>
    
int main()
{
	int i,N;
	unsigned long sum;
	
	/*read value of N*/
	printf("Enter the value of N: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
		sum= sum+ (i*i);
	
	/*print the sum*/
	
	printf("Sum of the series is: %ld\n",sum);
	
	return 0;
}

    Enter the value of N: 100
    Sum of the series is: 338350





3) C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!

/*  
This program will find the sum of Natural 
Number/Factorial of Number of all natural numbers from 1 to N.
*/

#include<stdio.h>
    
/*function to find factorial of the number*/
unsigned long factorial(int num)
{
	int i;
	/*always assign 1, if variable multiplies with values*/
	unsigned long fact=1;
	
	/*multiply num*num-1*num-2*..1*/
	for(i=num; i>=1; i--)
		fact= fact*i;
	
	/*return factorial*/
	return fact;
}

int main()
{
	int i,N;
	float sum;
	
	/*read value of N*/
	printf("Enter the value of N: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0.0f;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
		sum = sum + ( (float)(i) / (float)(factorial(i)) );
	
	/*print the sum*/
	
	printf("Sum of the series is: %f\n",sum);
	
	return 0;
}

    Enter the value of N: 10
    Sum of the series is: 2.718282



4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

/*  
C program to find sum of following series
 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
*/

#include<stdio.h>
    
int main()
{
	int i,N;
	float sum;
	
	/*read value of N*/
	printf("Enter the value of N: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0.0f;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
		sum = sum + ((float)1/(float)i);
	
	/*print the sum*/
	
	printf("Sum of the series is: %f\n",sum);
	
	return 0;
}

    Enter the value of N: 10
    Sum of the series is: 5.187378



5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms

/*  
C program to find sum of following series
 1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms
*/

#include<stdio.h>
#include<math.h>
    
int main()
{
	int i,N;
	float sum;
	int count;
	
	
	/*read value of N*/
	printf("Enter total number of terms: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0.0f;
	
	/*calculate sum of the series*/
	count=1;
	for(i=1;i<=N;i++)
	{
		sum = sum +  ( (float)(pow(count,2)) / (float)(pow(count,3)) );
		count+=2;
	}
	
	/*print the sum*/
	
	printf("Sum of the series is: %f\n",sum);
	
	return 0;
}

Enter total number of terms: 10
Sum of the series is: 2.133256









COMMENTS