NESTED FOR LOOP WITH SIMPLE PATTERN PROGRAMS.
Hi Everyone,
In my previous blog we have discussed about nested for loop and written some basic program in C . Today we will write some more programs like half pyramid by using special character * , Half Pyramid of number etc by using Nested for loops for better understanding . Before writing and understanding the Programs please go through my previous blog which will help you to understand in easy way. Below is the blog for your reference.
https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html
Let’s start with first program with Hallow Right Angle Triangle Program by using Nested for loop . In Program we use inner and outer loop which we discussed in my previous blog :
Below is the Program to get the result .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j;
for (i = 1; i <= 6; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
In above program j data type act as inner loop and print * and i act as number of rows needs to be printed with next line that’s why printf(“\n”) will work for outer loop and for inner j loop we use printf(“*”) . So in the above program in outer loop i work for rows and printing new line and inner loop print number of * according to j <= i; condition . . condition 6 i have given as per requirement for rows you can give any number or you can one more variable to pass for user input.
Below is the output of above program in compiler Code block
Inverted Right Angle triangle with special character * using Nested for loop by using row input from user.
Below is the Program .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k;
printf(" Please enter the number of rows: ");
scanf("%d", &k);
for (i = k; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Here we passing k as input parameter for rows and in condition we use decrement in outer loop for i variable and increment in inner loop for j variable so that it will display in inverted order , and through k variable user can give any numbers and loop will work display it accordingly . inner loop and print * and i act as number of rows needs to be printed with next line that’s why printf(“\n”) will work for outer loop and for inner j loop we use printf(“*”).
Output of Above Program in compiler Code block is as below :
Printing Number’s instead of special Character by using same logic .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k;
printf("Please enter the number of rows: ");
scanf("%d", &k);
for (i = 1; i <= k; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
In Above Program we applied the same logic only in printing we put %d for printing numbers and taking rows input from user.
Below is the output of above Program in Compiler code Block.
Hope you are able to understand printing pattern programs in C , for more practice i am writing one more same program with reversal order in numbers. Logic we applied the same for printing special character reversal program
Below is the Program :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &k);
for (i = k; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Logic we applied the same for printing special character reversal program only difference printing for number we use %d and increment and decrement as per requirement and number of rows passed by user. Inner and outer loop works as same which we used in another Program.
Output of the above Program is as below .
Hope you are able to understand the nesting for loop in C , and you can write any program related to Pattern and other programs for calculation and other things as per your requirement also see my previous blog where i have written some programs by using for loop and nested for loop for your reference. Please try to make more practice on nested for loop and try to make shape Pyramid , Heart etc . You may require one for loop to use so do that . Only thing you need to keep in mind Parent and child relations of loops which we discussed outer and inner loop which loop you want to execute in outer and you can use multiple inner in one parent that is outer loop. Once we start Array Topic nested for loop is very useful for program like Matrix , printing various numbers stored in Array etc.
In next blog i will come up with while do while as well as nested loop of both .
Please go through this blog very carefully as this is very important for programmer and nested loop vastly used in programming part which we will see in array, functions , pointer etc. Please Practice to write as much program as you can with your requirement.
In next blog i will come up with while do while as well as nested loop of while which is very similar for loop , but most programmer like for loop .
For any Doubt please let me know.
Thanks.
Very informative !
ReplyDelete