Write a C Program to display pattern with execution Explanation for Beginners.


Pattern:
*
**
***
****
*****

Solution:
Codebeautify.org Text to HTML Converter #include < stdio.h >
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
for(j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}


Explanation of how FOR loops are working internally:


i is for the number of Rows
j is for the number of Columns
We have declared “Rows” name variable for that value t is taken from the user.
Suppose User Enters 4 that means
Rows=4;


Value of iFor loop i Condition i<=4increment i++value of jFor Loop j condition j<=iincrement j++Output
11<=4 (TRUE)Incremented the value of i by 1 now i=211<=1 (TRUE)Incremented the value of J by 1 now j=2*
22<=1 (FALSE)Comes out of j for loop and reset the value of j to 1 now j=1*
22<=4 (TRUE)Incremented the value of i by 1 now i=311<=2 (TRUE)Incremented the value of J by 1 now j=2*
*
22<=2 (TRUE)Incremented the value of J by 1 now j=3*
**
33<=2 (FALSE)Comes out of j for loop and reset the value of j to 1 now j=1*
**
33<=4 (TRUE)Incremented the value of i by 1 now i=411<=3 (TRUE)Incremented the value of J by 1 now j=2*
**
*
22<=3 (TRUE)Incremented the value of J by 1 now j=3*
**
**
33<=3 (TRUE)Incremented the value of J by 1 now j=3*
**
***
44<=3 (FALSE)Comes out of j for loop and reset the value of j to 1 now j=1*
**
***
44<=4 (TRUE)Incremented the value of i by 1 now i=511<=4 (TRUE)Incremented the value of J by 1 now j=2*
**
***
*
22<=4 (TRUE)Incremented the value of J by 1 now j=3*
**
***
**
33<=4 (TRUE)Incremented the value of J by 1 now j=4*
**
***
***
44<=4 (TRUE)Incremented the value of J by 1 now j=5*
**
***
****
55<=4 (FALSE)Comes out of j for loop and reset the value of j to 1 now j=1*
**
***
****
55<=4 (FALESE)Comes out of i for loop



Write a C Program to display pattern with execution Explanation for Beginners. Write a C Program to display pattern with execution Explanation for Beginners. Reviewed by on September 04, 2019 Rating: 5

No comments:

Powered by Blogger.