Sunday, 13 October 2013

Pascal's Triangle

import java.io.*;
class Pascal
{
public void pascalw()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter a no.”);
int n=Integer.parseInt(br.readLine());
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)//loop evaluating the elements
{
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");//printing the Pascal Triangle elements
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}

No comments:

Post a Comment