i/p:
5
o/p:
1
12 2
11 13 3
10 15 14 4
9 8 7 6 5
i/p:
10
o/p:
1
27 2
26 28 3
25 45 29 4
24 44 46 30 5
23 43 54 47 31 6
22 42 53 0 48 32 7
21 41 52 51 50 49 33 8
20 40 39 38 37 36 35 34 9
19 18 17 16 15 14 13 12 11 10
import java.util.*;
public class NewClass20 {
void print(int ar[][],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
System.out.printf("%-2d ", ar[i][j]);
}System.out.println();
}
}
void triangularpat(int ar[][],int n)
{
int t=0,l=0,b=n-1,r=n-1,c=1;
while(t<b && l<r)
{
for(int i=t,j=l;i<=b&&j<=r;i++,j++)
{
ar[i][j]=c++;
}t++; r--;
for(int i=r;i>=l;i--)
{
ar[b][i]=c++;
}
r--;b--;
for(int i=b;i>=t;i--)
{
ar[i][l]=c++;
}
t++;l++;
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int ar[][]=new int[n][n];
NewClass20 ob=new NewClass20();
ob.triangularpat(ar, n);
ob.print(ar,n);
}
}
No comments:
Post a Comment