Page 492 - Cs_withBlue_J_C11_Flipbook
P. 492

Program 2      Write a program to input the numbers and print the pascals triangle.

                 1       // Print Pascal's Triangle in Java

                 2       import java.util.*;

                 3       class pascals
                 4        {
                 5           int findfactorial(int n)

                 6           {
                 7               int i,f=1;

                 8               for(i=1;i<=n;i++)
                 9               {

                10                   f=f*i;
                11               }

                12               return f;
                13           }

                14           public static void main(String[] args)
                15           {

                16               Scanner sc= new Scanner(System.in);
                17               int n , i, j;

                18               pascals pas = new pascals();
                19                 System.out.print("Enter the number of lines in the pascals triangle : ");

                20               n=sc.nextInt();
                21               for (i = 0; i <= n; i++)

                22               {
                23                   for (j = 0; j < n - i; j++)

                24                   {
                25                       System.out.print(" ");  // for giving space
                26                   }

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

                28                   {
                29                        System.out.print(" "+ pas.findfactorial(i)/ (pas.findfactorial(i
                                            - j)* pas.findfactorial(j))); // For printing
                30                   }

                31                   System.out.println();
                32               }

                33           } }


                490490  Touchpad Computer Science-XI
   487   488   489   490   491   492   493   494   495   496   497