Tuesday, January 24, 2012

Pascal triangle

import java.io.*;

import java.lang.*;

class PascalTriangle {

public static void main(String[] args) {

String inpstring = "";

InputStreamReader input = new InputStreamReader(System.in);

BufferedReader reader = new BufferedReader(input);

try

{

System.out.print("Enter number of rows for pascal triangle:");

inpstring = reader.readLine();

int n = Integer.parseInt(inpstring, 10);

for (int y = 0; y < n; y++)

{

int c = 1;

for(int q = 0; q < n - y; q++)

{

System.out.print(" ");

}

for(int x = 0; x <= y; x++)

{

System.out.print(" ");

System.out.print(c); // 3 digits

System.out.print(" ");

c = c * (y - x) / (x + 1);

}

System.out.println();

System.out.println();

}

System.out.println();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

No comments:

Post a Comment