Pattren
Pattren
#include<stdio.h>
int main()
{
int n=4,i,j,a=1,b,k,c=0,l;
b=n*n+1;
for(i=n;i>0;i--)
{
for(k=0;k<n-i;k++)
{
printf("-");
}
for(j=1;j<=i;j++)
{
printf("%d*",a++);
}
for(l=1;l<=i;l++)
{
printf("%d*",b++);
}
b=b+1-2*(i);
printf("\n");
}
}
Output:N=4
Here I took the input variable as n and assign a value 4. So, it takes four rows where each row is indexed
with the variable I, and the column with j and k is the variable used for spaces.
Here the 1st for loop repeats until from n to 1times, for each loop it gives k times of ‘-‘ symbol(k repeats
from 0 to n-i times) and the values are printed based on the conditions we have written in the for loop of j
and l, which repeats from 1 to i times.
In j for loop we increment the value of variable ‘a’ to 1 which is initialized to 1 and printed the value
before ‘*’. Here variable ‘a’ is used to print 1 to 10 while n=4.
In l for loop we increment the value of variable ‘b’ to 1 which is initialized to (n*n+1) in each row and
printed the value before ‘*’. Here variable ‘b’ is used to print 11 to 17 while n=4.
For example in the first row the value of b is incremented from 17 to 20 and in the second we need to
increment the value of b from 14 to n-1 so we assigned the value of b as b+1-2*(i) after completion of j
and l for loop. And again the I for loop starts in next line.