Program 48:
Output:
#include<stdio.h> main() { int i,j,num; printf("Enter number of rows\n"); scanf("%d",&num); for(i=1;i<=num;i++) { for(j=1;j<=num;j++) { printf("*"); } printf("\n"); } }Explanation:
- The program starts with initializing
- num → To store number of rows
- i ,j→ Temporary variable
-
printf("Enter number of rows\n"); scanf("%d",&num);
Taking number of rows from user. - Main Logic goes here:
for(i=1;i<=num;i++) { for(j=1;j<=num;j++) { printf("*"); } printf("\n"); }
- Lets take number of rows be 3.So num=3
- Iteration 1 of First Loop:i=1 and i<=3 which is true so 1st Loop execute
- Iteration 1 of 2nd Loop:j=1 and j<=3 which is true so iteration continues
- And prints '*' in 1st row and 1st coumn
- Now j will be incremented by 1
- So now j=2
- Iteration 2 of 2nd Loop:j=2 and j<=3 which is true so iteration continues
- And prints '*' in 1st row and 2nd coumn
- Now j will be incremented by 1
- So now j=3
- Iteration 3 of 2nd Loop:j=3 and j<=3 which is true so iteration continues
- And prints '*' in 1st row and 3rd coumn
- Now j will be incremented by 1
- So now j=4
- Iteration 4 of 2nd Loop:j=4 and j<=3 which is false so 2nd loop terminates
- So till now 1st row is printed
- Iteration 1 of 2nd Loop:j=1 and j<=3 which is true so iteration continues
- Iteration 2 of First Loop:i=2 and i<=3 which is true so 1st Loop execute
- Iteration 1 of 2nd Loop:j=1 and j<=3 which is true so iteration continues
- And prints '*' in 2nd row and 1st coumn
- Now j will be incremented by 1
- So now j=2
- Iteration 2 of 2nd Loop:j=2 and j<=3 which is true so iteration continues
- And prints '*' in 2nd row and 2nd coumn
- Now j will be incremented by 1
- So now j=3
- Iteration 3 of 2nd Loop:j=3 and j<=3 which is true so iteration continues
- And prints '*' in 2nd row and 3rd coumn
- Now j will be incremented by 1
- So now j=4
- Iteration 4 of 2nd Loop:j=4 and j<=3 which is false so 2nd loop terminates
- So till now 1st row and 2nd row are printed
- Iteration 1 of 2nd Loop:j=1 and j<=3 which is true so iteration continues
- Iteration 3 of 1st loop will also be same as above 2 iterations for 3rd row and finally all the 3 rows are printed as output
- Iteration 4 won't execute since i will be 4 and i<=3 i.e 4<=3 is not true
- Iteration 1 of First Loop:i=1 and i<=3 which is true so 1st Loop execute
Output: