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
- For the inner loop we will look how many times the stars are printing
- If we observe carefully we can see that for each line
- Stars are printing from i to n
- For i=1 , the stars are printing from 1to n i.e 9 from our example
- For i=4 , the stars are printing from 4 to n i.e 6 from our example
- So for the inner loop j will iterate from i to n
- Remember to print "\n" to make it 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=i;j<=n;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Note: scanf is used for taking user input

No comments:
Post a Comment