Iteration In C: More Examples On Nested For Loop

Examples On Nested For Loop

In this chapter we will see many different types of nested for loop examples.If you are not much aware about for loop please go through with the link
Understanding Iteration/Looping In C

Here we will practice some nested loop example to print some specific format.
These kind of examples will help to understand nested loop concepts.

#Program 1


Write a program to print given format.


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

#include <stdio.h>

void main() {

for (int i=1 ; i<=5; i++ ) {

  for (int j =1; j<=i; j++ ) {

     printf("%d ", j); 
  
  }

   printf ("\n");

}

}

#Explanation


i loop for number of rows and j for number of columns in each row.


i = 1,

j = 1 to i  , print j 

1

j loop ends

print new line

i loop ends


i = 2,

j = 1 to i  , print j 

1

1 2

j loop ends

print new line

i loop ends


i = 3,

j = 1 to i  , print j 

1

1 2

1 2 3

j loop ends

print new line

i loop ends

Same process will continue until i <=5

#Program 2

Write a program to print given format.


1
1 3
1 3 5
1 3 5 7 
1 3 5 7 9

#include <stdio.h>

void main() {

for (int i=1 ; i<=5; i++ ) {

  for (int j =1; j<=i*2; j= j+2 ) {

     printf("%d ", j); 
  
  }

  printf ("\n");

}


}

#Explanation


i loop for number of rows and j for number of columns in each row.


i = 1,

j = 1 to i *2 , print j    [ 1 * 2 = 2 , step 2] 

1

j loop ends

print new line

i loop ends


i = 2,

j = 1 to i*2   , print j    [ 2 * 2 = 4 , step 2] 

1

1 3

j loop ends

print new line

i loop ends


i = 3,

j = 1 to i*2   , print j    [ 2 * 3 = 6, step 2] 

1

1 3

1 3 5

j loop ends

print new line

i loop ends


Same process will continue until i <=5



#Program 3

Write a program to print given format.


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

#include <stdio.h>

void main() {

int max_start = 4, N=1;


for (int i=1 ; i<=7; i++ ) {


  for (int j =1; j<=N; j= j+1 ) {


     printf("\ '*' \"); 
  
  }


if (i < max_star)   

N++;

else

N --;

printf ("\n");

}

}


#Explanation


In this example max_star showing maximum number of '*' in row.

N is the counter variable for '*'

Once i = max_star then will decrease number of stars (N--).


#Program 4

Write a program to print given format.

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

#include <stdio.h>

void main() {

int  A = 65, E=69;

for (int i=1 ; i<=5; i++ ) {

  for (int j =A; j<=E; j= j++ ) {

     printf("%c ", j ); 
  
  }

  for (int space = 1; i< = i*5 ; space ++ ) {

     printf (" ");

  }

  for (int k =E; k>=A; k= k-- ) {

     printf("%c ", k ); 
  
  }
 
 E --;

 printf (" \n" );

}

}


#Explanation


i =1 


j = 65 to 69, print character value of j


A B C D E

end of j loop

next loop is for space printing. (we can set space accordingly) 

end of space loop

again another loop to print reverse value

k = 69 to 65, print character value of k

end of k loop


A B C D E  E D C B A


print next line

decrease value of E

end of i loop


i =2 

j = 65 to 68, print character value of j

A B C D E E D C B A 

A B C D


end of j loop

next loop is for space printing. (we can set space accordingly) 

end of space loop

again another loop to print reverse value

k = 69 to 65, print character value of k

end of k loop


A B C D E  E D C B A

A B C D      D C B A


print next line

decrease value of E

end of i loop 


Same process will continue till i completes its loop



Thanks for reading, please do share and comment your suggestions.

No comments

Powered by Blogger.