Wednesday, September 27, 2023

1.Square Pattern




I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9
  • Since we will take user input it can be simply n which will be taken from user
  • So now we know i will iterate from 1 to n
  • In this simple pattern the number of times the symbol is printed is same as n i.e 9
  • So j will also iterate from 1 to n
  • Remember to use \n to move to next line

Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(j=1;j<=n;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input

No comments:

Post a Comment