Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

// Java Program to Print Pascal's Triang

// Importing input output classes


import java.io.*;
// Main Class
public class GFG {

// Method 1
// To find factorial of a number
public int factorial(int a)
{
// Edge case
// Factorial of 0 is unity
if (a == 0)

// Hence return 1
return 1;

// else recursively call the fun


// number whose factorial is to
return a * factorial(a - 1);
}

// Method 2
// Main driver method
public static void main(String[] arg
{
// Declare and initialize number
// factorial is to be computed
int k = 4;

int a, b;

// Creating an object of GFG cla


// in the main() method
GFG g = new GFG();

// iterating using nested for lo


// matrix

// Outer for loop


for (a = 0; a <= k; a++) {

// Inner loop 1
for (b = 0; b <= k - a; b++)

// Print white space for


System.out.print(" ");
}

// Inner loop 2
for (b = 0; b <= a; b++) {

// nCr formula
System.out.print(
" "
+ g.factorial(a)
/ (g.factorial
* g.factori
}

// By now, we are done with


// a new line
System.out.println();
}
}
}

You might also like