Series Program in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Series are number sequences formed by applying specific rules and patterns to the numbers. They can be increased or decreased depending on the operation used to create them. A sequence 1, 2, 3, 4, ...., n is an increasing sequence obtained by adding 1 to the previous number. A sequence 1, 1/2, 1/3, 1/4, ...., 1/n is an example of decreasing sequence. A series is the sum of all the terms of a sequence.

We can use loops to find the sum of a series. A loop executes a single code block multiple times according to a given condition. To write the series program in C, we use for loop and while loop with appropriate sum statements. The loop condition helps us make the sequence increase or decrease.

Introduction

A series is an event or object that comes one after another and are of a similar kind. Here, we are talking about a series of numbers. A Series of numbers is a sequence formed out of the numbers by following specific rules and patterns. It can be either ascending or descending. Each successive number is obtained by either adding, subtracting, dividing, or multiplying the previous number by a specific number which can be constant sometimes. Series program in C gets programmed using for and while loops to create increasing and decreasing sequences of numbers with defined patterns and rules.

Let's look at a few sum of the series program in C.

Series Program in C

Sum of Series in C 1+2+3+4+5+..+N

Firstly, we find the sum of series 1,2,3,4,5,..., N, which is the sum of N natural numbers. Each successive value in the series gets obtained by adding 1 to the previous number. We can do this by using for loop with the condition to increase the number each time by one. It then gets added to give the final result.

Let us calculate the first sum of series 1+2+3+4+5+..+N using a series program in C.

In the above code, we have initialized a variable sum to 1. Then, we used a for loop to make an increasing sequence with a difference of 1. Each time the set of natural numbers gets added to the variable sum to find the sum of the series. The loop stops once the value of i reaches N. We then print the value sum, which gives the sum of the series program in C corresponding to N natural numbers.

Output

We can also calculate the sum of the above series of N natural numbers using the direct formula, i.e., N(N+1) / 2.

Output

Sum of Series in C Language 1² + 2² + 3² + 4² + 5² +….+ n²

Here, we will find the sum of series 1², 2², 3², 4², 5²,…., n². The pattern is to find the sum of squares of natural numbers up to n. Each successive value in the series gets obtained by adding 1 to the previous number and squaring it. We can do this by using for loop with the condition to increase the number each time by one. It then gets added to give the final result. Let us calculate the sum of series 1², 2², 3², 4², 5²,…., n² using a series program in C.

In the above code, we have initialized a variable sum to 1. Then, we used a for loop to make an increasing sequence with a difference of 1. Each time the square of natural numbers gets added to the variable sum to find the sum of the series. The loop stops once the value of i reaches N. We then print the value sum, which gives the sum of the series program in C corresponding to the square of N natural numbers.

Output

We can also calculate the sum of the above series of the square of N natural numbers using the direct formula, i.e., N(N+1)(2N+1) / 6.

Output

Sum of Series 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n

Here, we will find the sum of the series 1, 1/2, 1/3, 1/4, 1/5, 1/6, ……, 1/n. The pattern is to find the sum of the harmonic series in terms of 1/n from n = 1. Each successive value in the series gets obtained by adding 1 to the denominator of the number. Thus, it turns into a decreasing series. We can do this by using for loop with the condition to increase the denominator number each time by one. It then gets added to give the final result.

Let us calculate the sum of the series 1, 1/2, 1/3, 1/4, 1/5, 1/6, ……, 1/n using a series program in C.

We can also calculate the sum of series using the while loop. The while loop checks the condition of the statement and calculates the sum of the series.

In the above code, we have initialized a variable sum to 1. Then, we used a for loop and a while loop to make a decreasing harmonic sequence. Each number from the harmonic series gets added to the variable sum to find the sum of the series. The loop stops once the value of i reaches N. We then print the value sum, which gives the sum of the series program in C corresponding to the sum of N numbers in the harmonic series.

Output

Conclusion

  • We learned to calculate the sum of series using a C program.
  • The calculated series are 1+2+3+4+5+…+n, 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +……+ 1/n and 1 + 1/(2 * 2) + 1/(3 * 3) + 1/(4 * 4) + …… + 1/(n * n).
  • We calculated the sum of series using the for loop and the while loop in the C program.
  • The time complexities of the above sum of the series program in C is O(n) as the loop in the program runs n number of times.
  • The space complexities of the above sum of the series program in C is O(1) as the program does not use any extra space.