- First we count the number of times the pattern is printed i.e 9
- In this we have an exception that we won't iterate from 1to n but from 0 to n-1
- The outer for loop will iterate from 0 to n-1
- Before we used to iterate from 1 to n-i but now initial i value is 0
- So we will iterate from 1 to n-i-1
- Now for the main pattern for i=0 , stars printed is 1
- For i=1 stars printed are 3
- For i=2 stars printed are 5
- We can find a pattern that is (2*i)+1
- You can check by substituting the i value
- So j will iterate from 1 to (2*i)+1
- Remember to print "\n" to make it move to next line
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,s,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(s=1;s<=n-i-1;s++)
{
printf(" ");
}
for(j=1;j<=(2*i)+1;j++)
{
printf("*");
}
printf("\n");
}
getch();
}
Note:scanf is used for taking user input
No comments:
Post a Comment