Write a java program to print following number pattern
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
import java.util.Scanner;
public class NumberPattern {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Size: ");
int n=sc.nextInt();
int k=0,l=1;
for (int i = 1; i <= n; i++) {
if(i <= n/2+1) {
if(i!=1)
k=k+2;
} else {
if (k%2==0) {
k--;
} else {
k=k-2;
}
}
for (int j = 1; j <= n; j++) {
System.out.print((k*n+j)+" ");
}
System.out.println();
}
}
}
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
16 17 18 19 20
6 7 8 9 10
import java.util.Scanner;
public class NumberPattern {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Size: ");
int n=sc.nextInt();
int k=0,l=1;
for (int i = 1; i <= n; i++) {
if(i <= n/2+1) {
if(i!=1)
k=k+2;
} else {
if (k%2==0) {
k--;
} else {
k=k-2;
}
}
for (int j = 1; j <= n; j++) {
System.out.print((k*n+j)+" ");
}
System.out.println();
}
}
}
Comments
Post a Comment