We shall look at using for loop to find the sum of different number series.
(For the previous lesson, click here: for loop tutorial – Part 2)















Q.1. Design a loop to display the number series: 3, 7, 11, 15, 19, 23.
Solution.
We’ll make the loop counter start at 3, and have it go till 23. We’ll make it go in steps of 4.
Each time, within the loop, we’ll simply print the loop counter value.
for (int i = 3; i <= 23; i += 4) //loop goes from 3 to 23, in steps of 4.{printf("%d\n", i); //print the value of i}
output: 3711151923
Q.2. Design a for loop to compute the sum: 3 + 7 + 11 + 15 + 19 + 23.
Solution.
This is very similar to the above problem.
We’ll have a sum variable, and initialize it to 0.
We’ll make the loop counter go from 3 to 23, in steps of 4, just as before.
Earlier, we were printing the value of the loop counter. In this problem, instead of printing, we’ll keep adding the loop counter to our sum variable.
int sum = 0;
for (int i = 3; i <= 23; i += 4) //loop goes from 3 to 23, in steps of 4.
{
sum += i; //increment sum by i
}
printf("The desired sum is %d.", sum); //print the result
output: The desired sum is 78.
Q.3. Design a loop to find the sum of the 1st 10 terms of this series: 3 + 7 + 11 + …. .
Solution.
3 is 4×1 – 1.
7 is 4×2 – 1.
11 is 4×3 – 1, and so on.
So, in general, the i-th term is 4xi – 1.
We’ll initialize a variable called sum to 0.
We need to add 10 terms. So, we’ll make the loop counter i go from 1 to 10. Each time, within the loop, we’ll increment sum by (4*i – 1).
int sum = 0; for (int i = 1; i <= 10; i ++) //loop goes from 1 to 10. { sum += (4*i - 1); //increment sum by (4*i - 1)}printf("The desired sum is %d.", sum); //print the result
output : The desired sum is 210.
Q.4. Design a loop to compute the sum of the 1st n terms of the series: 3 + 7 + 11 + … . (n is input from the user.)
Solution.
It’s quite the same as above. Simply, instead of 10, we’ll make the loop run till n.
int sum = 0;
int n; //how many terms are there in the series?
printf("How many terms are there in the series? ");
scanf("%d", &n);
for (int i = 1; i <= n; i ++) //loop goes from 1 to n.
{
sum += (4*i - 1); //increment sum by (4*i - 1)
}
printf("The desired sum is %d.", sum); //print the result
sample output 1:How many terms are there in the series? 2The desired sum is 10. sample output 2: How many terms are there in the series? 5 The desired sum is 55.
Q.5. Design a loop to find the sum: 1 + 2 + 3 + … (till n terms). (Here n is an input from the user.)
Solution.
This is similar to the above problem.
We’ll make the loop go from 1 to n, and in each iteration, increment our sum variable by the loop counter.
int sum = 0;int n; //how many terms are there in the series? printf("How many terms are there in the series? ");scanf("%d", &n); for (int i = 1; i <= n; i ++) //loop goes from 1 to n. { sum += i; //increment sum by i}printf("The desired sum is %d.", sum); //print the result
sample output 1: How many terms are there in the series? 3The desired sum is 6. sample output 2: How many terms are there in the series? 5 The desired sum is 15.
In this lesson, we looked at using for loop to compute the sum of different number series. (For more on C Programming, you can see here.)
Leave a comment