Write a Java program to print the following number pyramid:
public class MyPyramid
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value ");
int n = sc.nextInt();
for (int i = 0; i < n; i++)
{
for (int spc = n - i; spc > 0; spc--)
{
System.out.print(" ");
}
for (int j = 0; j <= i; j++)
{
System.out.print((i + 1) + " ");
}
System.out.println();
}
}
}
Output:
Enter the value
6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
BUILD SUCCESSFUL (total time: 3 seconds)
Java Resources are available for Download from below
Category: Interview, Interview Question, Java, Pyramid
0 comments