Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PascalTriangle.java #662

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions PascalTriangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//Given the num of rows numRows, generate the first numRows of Pascal's Triangle.
import java.util.ArrayList;
import java.util.Scanner;

public class PascalTriangle {

static ArrayList<ArrayList<Integer>> generate(int numRows){
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(numRows==0)
{
return result;
}

ArrayList<Integer> pre = new ArrayList<>();
pre.add(1);
result.add(pre);
for(int i=1;i<=numRows-1;i++)
{
ArrayList<Integer> cur = new ArrayList<>();
cur.add(1); //first
for(int j=0;j<pre.size()-1;j++)
{
cur.add(pre.get(j) + pre.get(j+1)); //middle
}
cur.add(1); //last
result.add(cur);
pre=cur;
}
return result;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Program to print the pascal triangle, given the no of rows!!");
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
ArrayList<ArrayList<Integer>> list = generate(n);
for(ArrayList<Integer> temp : list)
{
System.out.println(temp);
}
}
}