Here, we shall look at a few simple problems for printing number series, and see how to design for loop to solve them.
(For the previous tutorial in this series, click here: for loop tutorial – Part 1)
(For more on C Programming, see here: posts on C Programming)
















We looked at some simple for loops above.
Things to keep in mind when designing a loop –
- how many times should the loop run?
- what task is to be performed each time the loop runs?
To control how many times the loop runs, we use the loop control variable or the loop counter.
e.g. for (int i = 1; i <= 6; i++)
This indicates that the loop control variable (which we are calling as i here) starts with the value as 1. The loop runs until i reaches the value of 6. (The loop terminates when i goes above 6.) And i increases by 1 in each iteration of the loop.
int i = 1. This says that the initial value of the loop counter is 1.
i <= 6. This says that the loop runs as long as i is less than or equal to 6.
i++. This says that after each iteration of the loop (that is, after each time that the loop runs), i gets incremented by 1.
So, in all the loop runs 6 times. (In the 1st iteration, i is 1. In the 2nd iteration, i is 2. And so on.)
What is the task to be repeatedly performed.? That is, what is the task to be performed each time the loop runs?
Whatever that task is, we shall write it within the body of the loop.
Q.1. Design a for loop to display “Have a nice day!” 8 times.
Solution.
We’ll make the loop run 8 times, and each time, we shall print “Have a nice day!”.
for (int i = 1; i <= 8; i++){ printf("Have a nice day!\n"); }
output: Have a nice day!Have a nice day!Have a nice day!Have a nice day!Have a nice day!Have a nice day!Have a nice day!Have a nice day!
Q.2. Design a loop to print the number 10 six times.
Solution.
We’ll make the for loop run 6 times, and each time, we shall print the number 10 once.
for (int j = 1; j <= 6; j++){ printf("%d \n", 10); }
output: 101010101010
Q.3. Design a for loop to display the series 2, 3, 4, 5, 6, 7.
Solution.
We’ll make the loop start with the loop counter (let’s call it i) as 2. We’ll make i go till it becomes 7. We’ll increment i by 1 after each iteration of the loop.
What is the task to be repeatedly performed? We’ll print the value of i each time we are inside the loop.
for (int i = 2; i <= 7; i++) //i goes from 2 to 7, incrementing by 1 each time. { printf ("%d\n", i); //print the value of i }
output: 234567
Q.4. In the above code, change the printf statement to printf(“%d\n”, 2*i); . How will the output change?
Solution.
The loop counter goes from 2 to 7 as before. Just that this time, we shall print 2*i in each iteration of the loop, instead of i.
for (int i = 2; i <= 7; i++) { printf ("%d\n", 2*i); //print 2*i each time. }
output: 468101214
Q.5. Write another loop to print the same series: 4, 6, 8, 10, 12, 14.
solution.
Let’s have the loop control variable i start at 4. Let i go till it becomes 14. Let us increment i by 2 after each iteration of the loop.
And simply print the value of i in each iteration of the loop.
so, we get the following code:
for (int i = 4; i <= 14; i+=2) //i goes as 4, 6, 8, ... till 14. { printf ("%d \n", i); //print value of i }
output:468101214
Q.6. Design a loop to print the series: 9, 16, 25, 36.
solution.
Each number to be printed is a square number.
9 is 3^2. (3 squared.)
16 is 4^2.
25 is 5^2.
36 is 6^2.
we’ll design a loop that runs 4 times. The loop counter i will go from 3 to 6. After each iteration, the loop counter will get incremented by 1.
In each iteration of the loop, we shall print i squared.
so, we get the following code:
for (int i = 3; i <= 6; i++) //loop counter goes from 3 to 6, increasing by 1 each time. { printf("%d \n", i*i); //print i squared }
output: 9162536
Q.7. Design a loop to display the series 16, 36, 64, 100, 144.
solution.
Each number is a square number.
16 is 4 squared.
36 is 6 squared.
64 is 8 squared.
100 is 10 squared.
144 is 12 squared.
We can observe the pattern and design the loop.
We’ll have the loop counter i start at 4, and have it go till 12. And after each iteration of the loop, we shall increment i by 2. (This way, i will get the values 4, 6, 8, 10, 12)
Within the body of the loop, we shall print i squared.
for (int i = 4; i <= 12; i+=2) //have i go from 4 to 12. Increases by 2 each time. { printf ("%d \n", i*i ); //print i squared. }
output : 163664100144
In this class, we saw how to design for loops for displaying certain simple number series.
(For more on C Programming, see here. )









































































