Program 50:
Output:
#include<stdio.h> main() { int i,j,num; printf("Enter number of rows\n"); scanf("%d",&num); for(i=num;i>=1;i--) { for(j=i;j>=1;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=num;i>=1;i--) { for(j=i;j>=1;j--) { printf("*"); } printf("\n"); }
- Lets take number of rows be 3.So num=3
- Iteration 1 of First Loop:i=num→i=3 and i>=1 which is true so 1st Loop execute
- Iteration 1 of 2nd Loop:j=i→j=3 and j>=1 which is true so iteration continues
- And prints '*' in 1st row and 1st coumn
- Now j will be decremented by 1
- So now j=2
- Iteration 2 of 2nd Loop:j=2 and j>=1 which is true so iteration continues
- And prints '*' in 1st row and 2nd coumn
- Now j will be decremented by 1
- So now j=1
- Iteration 3 of 2nd Loop:j=1 and j>=1 which is true so iteration continues
- And prints '*' in 1st row and 3rd coumn
- Now j will be decremented by 1
- So now j=0
- Iteration 4 of 2nd Loop:j=0 and j>=1 which is false so 2nd loop terminates
- So till now 1st row is printed
- Iteration 1 of 2nd Loop:j=i→j=3 and j>=1 which is true so iteration continues
- Iteration 2 of First Loop:i=2 and i>=1 which is true so 1st Loop execute
- Iteration 1 of 2nd Loop:j=i→j=2 and j>=1 which is true so iteration continues
- And prints '*' in 2nd row and 1st coumn
- Now j will be decremented by 1
- So now j=1
- Iteration 2 of 2nd Loop:j=1 and j>=1 which is true so iteration continues
- And prints '*' in 2nd row and 2nd coumn
- Now j will be decremented by 1
- So now j=0
- Iteration 3 of 2nd Loop:j=0 and j>=1 which is false so 2nd loop terminates
- So till now 1st row and 2nd row are printed
- Iteration 1 of 2nd Loop:j=i→j=2 and j>=1 which is true so iteration continues
- Iteration 3 of First Loop:i=1 and i>=1 which is true so 1st Loop execute
- Iteration 1 of 2nd Loop:j=i→j=1 and j>=1 which is true so iteration continues
- And prints '*' in 3rd row and 1st coumn
- Now j will be decremented by 1
- So now j=0
- Iteration 2 of 2nd Loop:j=0 and j>=1 which is false so 2nd loop terminates
- So all the 3 rows are printed
- Iteration 1 of 2nd Loop:j=i→j=1 and j>=1 which is true so iteration continues
- Iteration 4 of First loop:i=0 and i>=1 is false so First Loop also terminates
- Iteration 1 of First Loop:i=num→i=3 and i>=1 which is true so 1st Loop execute
Output: