Java Program to Print Pascal Triangle

Write a Java program to print a Pascal triangle using for loop.

import java.util.Scanner;

public class PascalTriangleNumber1 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Pascal Triangle Number Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Pascal Triangle Number Pattern");
		
		for (int i = 0 ; i < rows; i++ ) 
		{
			int number = 1;
			System.out.printf("%" + (rows - i) * 2 + "s", "");
			
			for (int j = 0 ; j <= i; j++ ) 	
			{
				System.out.printf("%4d", number);
				number = number * (i - j) / (j + 1);
			}
			System.out.println();
		}
	}
}
Java Program to Print Pascal Triangle

This example displays the Pascal triangle of numbers using a while loop.

import java.util.Scanner;

public class PascalTriangleNumber2 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Rows = ");
		int rows = sc.nextInt();
		
		int number, j, i = 0;
		
		while(i < rows ) 
		{
			number = 1;
			System.out.printf("%" + (rows - i) * 2 + "s", "");
			j = 0 ; 
			while (j <= i ) 	
			{
				System.out.printf("%4d", number);
				number = number * (i - j) / (j + 1);
				j++;
			}
			System.out.println();
			i++;
		}
	}
}
Enter 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

Java Program to print Pascal triangle using do while loop.

import java.util.Scanner;

public class PascalTriangleNumber3 {
	
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Rows = ");
		int rows = sc.nextInt();
		
		int number, j, i = 0;
		
		do
		{
			number = 1;
			System.out.printf("%" + (rows - i) * 2 + "s", "");
			j = 0 ; 
			
			do	
			{
				System.out.printf("%4d", number);
				number = number * (i - j) / (j + 1);

			} while (++j <= i );
			System.out.println();

		} while(++i < rows );
	}
}
Enter Rows = 12
                           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   8  28  56  70  56  28   8   1
         1   9  36  84 126 126  84  36   9   1
       1  10  45 120 210 252 210 120  45  10   1
     1  11  55 165 330 462 462 330 165  55  11   1

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.