Vous êtes sur la page 1sur 1

PATTERN PROGRAM

#include<stdio.h>
int main()
{
int n=5,i,j,k,a=1;
for(i=1;i<=n;i++)
{
for(k=i;k<n;k++)
{
printf(" ");
}

for(j=1;j<=2*i-1;j++)
{
if(j<=i)
{ printf("%d",a);
if(j<i)
a+=1;
}
else
printf("%d",--a);
}
printf("\n");
}
return 0;
}

Output:

Explanation:
Here I took the input variable as n and assign a value 5. So, it takes five 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 1 to n times, for each loop it gives k spaces(k repeats from I to n
times) and the values are printed based on the conditions we have written in the for loop of j, which the j
for loop repeats from 1 to 2*i-1 times.
Due to increment and decrement I took another variable ‘a’ and assign value 1. Then ‘a’ value is first
printed until I equals to j and incremented to 1 until j is less than i and if both the conditions fail ‘a’ is
decremented to 1 and printed.
After complete of j loop the i loop starts from next line.

Vous aimerez peut-être aussi