Star triangle frame pyramid program in Java.





package demo;
import java.util.Scanner;
public class TriangleFrame
{
 public static void main(String[] args) 
 {
  int num, n;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter number of rows: ");
  num = sc.nextInt();
  System.out.println();
  n = num;
  for (int r = 1; r <= num; r++) 
  {
   for (int sp = 1; sp <= r; sp++) {
    System.out.print(" ");
   }
   for (int c = 1; c <= n; c++) {
    System.out.print("*");
   }
   for (int c = num - r; c >= 1; c--) {
    System.out.print("*");
   }
   n--;
   System.out.println();
  }
  for (int r = 2; r <= num; r++) {
   for (int sp = num - r + 1; sp >= 1; sp--) {
    System.out.print(" ");
   }
   for (int c = 1; c <= r; c++) {
    System.out.print("*");
   }
   for (int c = r - 1; c >= 1; c--) {
    System.out.print("*");
   }
   System.out.println();
  }
 }
}

Output:

Enter number of rows: 4

 *******
  *****
   ***
    *
   ***
  *****
 *******

BUILD SUCCESSFUL (total time: 5 seconds)


Note: Hello friends, If you have any better solution for this programs then please comment below, I will add your comment as a solution for this programs.

Thanks for visiting my Blog.


No comments:

Post a Comment