Skip to content

Commit 0a14a84

Browse files
Java Program for ZigZag Pattern
1 parent c9a2bd7 commit 0a14a84

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

ZigZag.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.lang.*;
2+
import java.util.*;
3+
public class ZigZag{
4+
public static void main(String arg[])
5+
{
6+
int n,k;
7+
Scanner in = new Scanner(System.in);
8+
System.out.println("Enter the number till you want to print the numbers");
9+
n = in.nextInt();
10+
System.out.println("Enter the no. of rows");
11+
k = in.nextInt();
12+
String grid[][] = new String[k][n];
13+
for(int i=0;i<k;i++)
14+
{
15+
for(int j=0;j<n;j++)
16+
{
17+
grid[i][j]=" ";
18+
}
19+
}
20+
for(int i=0;i<n;i++)
21+
{
22+
int m = k-1;
23+
int colm = m-Math.abs(i%(2*m)-m);
24+
grid[colm][i]=Integer.toString(i+1);
25+
}
26+
for(int i=0;i<k;i++)
27+
{
28+
for(int j=0;j<n;j++)
29+
{
30+
System.out.print(grid[i][j]);
31+
}
32+
System.out.println();
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)