Contact Learn C
Copy

Program 48:Symbol Pattern 1

Program 48:
#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:

  1. The program starts with initializing
    • num → To store number of rows
    • i ,jTemporary variable
  2. printf("Enter number of rows\n");
     scanf("%d",&num);
    Taking number of rows from user.
  3. Main Logic goes here:
    for(i=1;i<=num;i++)
     {
     
      for(j=1;j<=num;j++)
      {
       printf("*");
         
      }
      
     printf("\n");
     }
  4. 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  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  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  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 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  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  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  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 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


 Output:

Symbol Pattern 1
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide