Write a java program to print following pattern when n=5:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
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();
for (int i = 1; i <=2*n-1; i++) {
int k=1;
if(i<=n) {
for(int j=1;j<=i;j++) {
System.out.print(k+" ");
k++;
}
}
else {
for(int j=1;j<2*n-i+1;j++) {
System.out.print(k+" ");
k++;
}
}
System.out.println();
}
}
}
Comments
Post a Comment