Tuesday, 20 January 2015

Java Program to print a triangle with specific number


This is a Java Program to print a triangle filled with a specific number.



Java Program to print a triangle with a specific number


PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class TriangleWithNumbers {
 public static void main(String[] ars) {
  int i, j, n, k, num;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter number of lines[height of triangle] : \t");
  n = scan.nextInt();
  System.out.print("With which number do you want to fill the triangle : \t");
  num = 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(num);
   }
   System.out.print("\n");
  }
 }
}


OUTPUT : 
Java Program to print a triangle with a specific number

No comments:

Post a Comment