C For Loop Examples: Practical Applications in Programming

Discover practical examples of the for loop in C programming. In this section, we will demonstrate how to create a program that counts to 100 in increments of ten, showcasing the versatility and efficiency of the for loop for iterative tasks. Learn how to apply this fundamental concept in your own coding projects.



C For Loop Examples

Real-Life Examples

To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:

Syntax

for (i = 0; i <= 100; i += 10) {
    printf("%d\n", i);
}
Output

0
10
20
30
40
50
60
70
80
90
100

In this example, we create a program that only prints even numbers between 0 and 10 (inclusive):

Syntax

for (i = 0; i <= 10; i = i + 2) {
    printf("%d\n", i);
}
Output

0
2
4
6
8
10

Here we only print odd numbers:

Syntax

for (i = 1; i < 10; i = i + 2) {
    printf("%d\n", i);
}
Output

1
3
5
7
9

In this example, we print the powers of 2 up to 512:

Syntax

for (i = 2; i <= 512; i *= 2) {
    printf("%d\n", i);
}
Output

2
4
8
16
32
64
128
256
512

And in this example, we create a program that prints the multiplication table for a specified number:

Syntax

int number = 2;
int i;

// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
    printf("%d x %d = %d\n", number, i, number * i);
}
Output

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20