forked from jvm-coder/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZag.java
More file actions
35 lines (35 loc) · 911 Bytes
/
ZigZag.java
File metadata and controls
35 lines (35 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.lang.*;
import java.util.*;
public class ZigZag{
public static void main(String arg[])
{
int n,k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number till you want to print the numbers");
n = in.nextInt();
System.out.println("Enter the no. of rows");
k = in.nextInt();
String grid[][] = new String[k][n];
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
grid[i][j]=" ";
}
}
for(int i=0;i<n;i++)
{
int m = k-1;
int colm = m-Math.abs(i%(2*m)-m);
grid[colm][i]=Integer.toString(i+1);
}
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(grid[i][j]);
}
System.out.println();
}
}
}