Title

JAVA

      In this blog, you will find programs on JAVA and also some help on how to write programs for JAVA...

December 18, 2014

Pascal's Triangle

Sample Output: n=5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1 


import java.util.Scanner;
public class PT
{
    public static void main()
    {
        Scanner s=new Scanner(System.in);
        System.out.println("Please enter value for n");
        int n=s.nextInt();
        int pas[]=new int [n+1];
        pas[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0; j<=i; j++)
            {
                System.out.print(pas[j] + " ");
            }
            System.out.println();
            for(int k=i+1; k>=1; k--)
            {
                pas[k]=pas[k]+pas[k-1];
            }
        }
    }
}

No comments:

Post a Comment

Please write your comment in the box below...