Program to Print a Pyramid of Numbers up to given Lines
Hello friends, in this tutorial we are going to write a program to print pyramid of Numbers up to of given lines. So, open your IDE and make a new Project. Now, write the following code in Main.java file.
Main.java :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int rows;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows to print : ");
rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = (rows - i); j >= 1; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
public class Main {
public static void main(String[] args) {
int rows;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of rows to print : ");
rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = (rows - i); j >= 1; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Your output will be like this.
Output :
C:/Users/Username/Desktop>Enter the number of rows you want to print :
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
5
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5