Nearby lessons

38 of 72

C - Nested Loops

Nested Loops is one of the foundational topics in C programming. This lesson explains Nested Loops with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Pattern 1: Repeated Number Sequence

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= 5; j++) {
9 printf("%d ", j);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
  

Pattern 2: Repeated Row Number

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= 5; j++) {
9 printf("%d ", i);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
  

Pattern 3: Increasing Number Triangle

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= i; j++) {
9 printf("%d ", j);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
  

Pattern 4: Number Triangle with Repeated Rows

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= i; j++) {
9 printf("%d ", i);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
  

Pattern 5: Square Star Pattern

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= 5; j++) {
9 printf("* ");
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

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

Pattern 6: Increasing Star Triangle

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= i; j++) {
9 printf("* ");
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

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

Pattern 7: Repeated Alphabet Sequence

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= 5; j++) {
9 printf("%c ", 'A' + j - 1);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

A B C D E
A B C D E
A B C D E
A B C D E
A B C D E
  

Pattern 8: Increasing Alphabet Triangle

Example08
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= i; j++) {
9 printf("%c ", 'A' + j - 1);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

A
A B
A B C
A B C D
A B C D E
  

Pattern 9: Alphabet Triangle with Repeated Rows

Example09
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i, j;
6 
7 for (i = 1; i <= 5; i++) {
8 for (j = 1; j <= i; j++) {
9 printf("%c ", 'A' + i - 1);
10 }
11 printf("\n");
12 }
13 
14 return 0;
15}
Output

A
B B
C C C
D D D D
E E E E E
  
📝 Key Takeaways
  • while checks first; do-while runs at least once; for gathers start/condition/update.
  • for is best when you know the number of iterations.
  • break stops the loop; continue skips one round.
  • Nested loops: inner loop completes for each outer round.
  • Pattern programs = outer loop (rows) + inner loop (columns).
  • Never forget the update (i++) — else you get an infinite loop.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1