Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Print Pascals triangle using java program

  • Pascals triangle means arranging numbers in pascal triangle format.
  • First row starts with number 1.
  • Here is the pascal triangle with 8 rows.



  •  The sum of all numbers in each row will be double the sum of all numbers in above row
  •  The diagonals adjacent to the border diagonals of 1's contains natural numbers in order


 Program #1: Java example program to print  numbers in pascals triangle pattern.

  1. package interviewprograms.instanceofjava;

  2. import java.util.Scanner;
  3. /*
  4.  * www.instanceofjava.com
  5.  */
  6. public class PasclasTriangleProgram {

  7. public static void main(String args[]){
  8. Scanner in = new Scanner(System.in);
  9. System.out.println("Enter number of rows ");

  10. int rows= in.nextInt();

  11.  for(int i =0;i

  12.         int number = 1;
  13.   
  14.   System.out.format("%"+(rows-i)*2+"s","");

  15.  for(int j=0;j

  16.     System.out.format("%4d",number);

  17.       number = number * (i - j) / (j + 1);

  18. }

  19.   System.out.println();

  20. }

  21. }
  22. }        

Output:

  1. Enter number of rows 
  2. 8
  3.                    1
  4.                  1   1
  5.                1   2   1
  6.              1   3   3   1
  7.            1   4   6   4   1
  8.          1   5  10  10   5   1
  9.        1   6  15  20  15   6   1
  10.      1   7  21  35  35  21   7   1

1.Pattern Programs in java Part-1

2.Pattern Programs in java Part-2

3.Pattern Programs in java Part-3 


This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

Print Pascals triangle using java program

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×