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

2 Comments

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Best selenium training institute in Pune

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)