Programs for printing pyramid patterns in Java


import java.util.Scanner;


public class First2 {


public void p1() {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
for (int i = 1; i <= row; i++) {
for (int j = i; j < row; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}

}

public void p2() {
Scanner sc = new Scanner(System.in);
int rows1 = sc.nextInt();
for (int i = 1; i <= rows1; i++) {
for (int j = i; j < rows1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}

System.out.println();

}
for (int i = rows1; i >= 1; i--) {
for (int j = i; j < rows1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}

System.out.println();

}

}

public void p3() {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
for (int i = 1; i <= row; i++) {
for (int j = i; j < row; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}

System.out.println();
}
}

public static void main(String[] args) {
First2 f = new First2();
f.p1();
f.p2();
f.p3();
// f.ArithError();

}
}


output
-----------
3
  *
 ***
*****
4
   *
  ***
 *****
*******
*******
 *****
  ***
   *

5
    *
   ***
  *****
 *******
*********

Post a Comment

0 Comments