Program To Print Pascal’s Triangle in Java

pascal triangle pattern program in c
import java.util.*;  
class Main{
    public static void main(String ...a){
    int temp=1,i,j,k;
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter number of rows: ");  
    int n= sc.nextInt();  
    for (i=0; i<n; i++) {
        for (k=1; k <= n-i; k++)
        {
            System.out.print(" ");
        }
        for (j=0; j<=i; j++) {
            if (j==0 || i==0)
                temp = 1;
            else
                temp=temp*(i-j+1)/j;
            System.out.print(" "+temp);
           }
        System.out.println("");
	    }
    }
} 

Output

Enter number of rows: 5
      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1

Leave a Comment