Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Wednesday 14 February 2018

How to : print Pascal triangle in java

import java.util.Scanner;

public class pascal {

 static int fact(int t)

{

if(t==1 || t==0)

return 1;

else

return t*fact( t - 1 );

 }

 public static void main(String[] args)

 {

  Scanner sc=new Scanner(System.in);

 System.out.println("Enter the no of rows:");

  int n=sc.nextInt();

  int i,j;

  for (i = 0; i <= n; i++)

     {

        for (j = 0; j<= (n - i  - 2); j++)

          System.out.print(" ");

  

       for (j = 0 ; j <= i; j++)

           {

        int x=(fact(i)/(fact(j)*fact(i-j)));

         System.out.print(x+ " ");

           }

        System.out.println();

     }

   }

}

 Example of Pascal Triangle---


No comments:

Post a Comment