Wednesday, 22 June 2016

Program to print a Number Pattern Type-28(Pascal Triangle).

Explanation :
If n =6 then output will be
              1
            1  1
          1  2  1
        1  3  3  1
     1  4   6   4  1
    1 5 10 10  5  1.

Code:

import java.util.Scanner;
public class Pattern28
{
    public static void main( String args[])
    {
        int line,i,j;
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the no. of lines: ");
        line=sc.nextInt();
       
    
        for(i=0;i<line;i++){
             for(j=0;j<line-i-1;j++)
                 System.out.printf(" ");
    
             for(j=0;j<=i;j++)
                 System.out.print(fact(i)/(fact(j)*fact(i-j)));
             System.out.printf("\n");
        }
    }
    
   public static long fact(int num){
        long f=1;
        int i=1;
        while(i<=num){
             f=f*i;
             i++;
      }
      return f;
     }
}

No comments:

Post a Comment