- 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.
- package interviewprograms.instanceofjava;
- import java.util.Scanner;
- /*
- * www.instanceofjava.com
- */
- public class PasclasTriangleProgram {
- public static void main(String args[]){
- Scanner in = new Scanner(System.in);
- System.out.println("Enter number of rows ");
- int rows= in.nextInt();
- for(int i =0;i<rows;i++) {
- int number = 1;
- System.out.format("%"+(rows-i)*2+"s","");
- for(int j=0;j<=i;j++) {
- System.out.format("%4d",number);
- number = number * (i - j) / (j + 1);
- }
- System.out.println();
- }
- }
- }
Output:
- Enter number of rows
- 8
- 1
- 1 1
- 1 2 1
- 1 3 3 1
- 1 4 6 4 1
- 1 5 10 10 5 1
- 1 6 15 20 15 6 1
- 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
No comments