Pages

Saturday, 26 January 2019

Pascal Triangle

Q: Write a program to make pascal triangle ?

Answer :

Pascal Triangle :

Pascal's Triangle is more than just a big triangle of numbers. There are two major areas where Pascal's Triangle is used, in Algebra and in Probability / Combinatorics.


Rule 

  1. At the top center rach number will be “1.”
  2. On the next row write two 1’s, forming a triangle.
  3. On each next row start and end with 1’s and compute each interior term by adding the two numbers above it.

Code : 


import java.util.*;
public class pascal
{
   
    static void sop(String ar)
    {
        System.out.println(ar);
    }
   public static void main(String args[])
   {
       int i,j,L,sp,number;
       Scanner sc=new Scanner(System.in);
       sop("Enter triangle size");
       i=sc.nextInt();
       sp=i;
       for(j=0;j<=i;j++)
       {
           for(L=1;L<=sp;L++)
            System.out.print(" ");
           for(L=0,number=1;L<=j;L++)
           {
               System.out.print(number +" ");
               number=number*(j-L)/(L+1);
            }

          sop("");
           sp--;
       }
     
   
    }
}

No comments:

Post a Comment