You are on page 1of 2

/* JAVA PROGRAM TO GENERATE A PASCALS TRIANGLE WITH OUTPUT AS BELOW

EACH NODE IS SUM OF PARENT NODES, EXCEPT THE LHS AND RHS CORNER NODES.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

NOTE: TO GENERATE A PASCALS TRIANGLE, YOU NEED TO MAKE USE OF RECURSION ALGORITH
M AND NOT ITERATIVE PROGRAM
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class PascalsTriangle
{
public static void PascalsGenerator() {
for (int i=0;i<7;i++) {
for (int j=0;j<=i;j++){
System.out.print(check(i)/(check (j)*check(i-j))
+ " ");
}
System.out.println("\n");
}
}
public static int check(int a)
{
if(a==1||a==0)
return 1;
return a*check(a-1);
}
public static void myMethod( int counter)
{
if(counter == 0)
return;
else
{
System.out.println(""+counter);
myMethod(--counter);
return;
}
}
public static void main(String []args)
{
PascalsTriangle.PascalsGenerator();
}
}
class SimplePascals
{
public static void main(String[] args) throws IOException
{
System.out.println("Enter the number of rows for which the Pasca
l Triangle is requird: ");
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader bf = new BufferedReader(is);
int numRow = Integer.parseInt(bf.readLine());
for (int i=1; i<=numRow; i++) {
//Prints the blank spaces
for (int j=1; j<=numRow-i; j++) {
System.out.print(" ");
}
//Prints the value of the number
for (int k=1; k<=i; k++) {
System.out.print( i + " ");
}
System.out.println();
}
}
}

You might also like