Steps
- Let's count the number of lines i.e 9 so we take it as n
- So i will iterate from 1 to n
- The pattern printed never exceeds i
- For i=1 it is 1, i=9 pattern is there till 9
- So j loop will iterate from 1 to i
- Now coming to the pattern
- We will see where numbers are being printed
- First vertical line i.e for j=1 each time
- Next horizontal line i.e i=n (i=9 in our example)
- Finally there is a diagonal values 1==1,2==2 so it is i=j
- We will use these conditions in a if condition using logical or
- A difference we see in this pattern is that stars are not being printed
- If we observe the numbers carefully we can understand those are just j values
- So true part will print j values
- False part will print space
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<=i;j++)
{
if(i==n || j==1 || i==j)
{
printf("%d",j);
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
}
Note: scanf is used for taking user input