Tuesday, 20 January 2015

Java Program to print a triangle filled with stars



This is a Java Program which  prints out a triangle filled with stars.

Java Program to print a triangle filled with stars

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class TriangeWithStars {
 public static void main(String[] args) {
  int i, j, n, k;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter number of lines[height of triangle] : \t");
  n = scan.nextInt();
  scan.close();
  for (i = 1; i <= n; i++) {
   for (j = i; j < n; j++) {
    System.out.print(" ");
   }
   for (k = 1; k < i * 2; k++) {
    System.out.print("*");
   }
   System.out.print("\n");
  }
 }
}

OUTPUT :
Java Program to print a triangle filled with stars