C For Loop Examples

C Tutorial

C For Loop Examples (Beginner to Advanced)

In C For Loop Examples is used when you know in advance how many times a loop should run.

1. Print Numbers from 1 to 10


 

Output:

1 2 3 4 5 6 7 8 9 10

2. Print Even Numbers from 2 to 20


 

Output:

2 4 6 8 10 12 14 16 18 20

3. Sum of First N Numbers


 

Sample Input/Output:

Enter a number: 5
Sum = 15

4. Factorial of a Number


 

Sample Input/Output:

Enter a number: 5
Factorial of 5 = 120

5. Multiplication Table


 

Sample Input/Output:

Enter a number: 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70

6. Nested for Loop Example – Pattern

Print a right-angle triangle:


 

Output:

*
* *
* * *
* * * *
* * * * *

7. Key Points

  1. for loops are ideal when the number of iterations is known.

  2. Initialization, condition, and increment/decrement are in one line.

  3. Can be nested for patterns, tables, or multi-dimensional tasks.

  4. Loop variable can be declared inside or outside the loop.

You may also like...