Skip to content

Instantly share code, notes, and snippets.

@DanyelMorales
Created July 5, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanyelMorales/da42cbc0dab10bcdf4b7eefa9208565c to your computer and use it in GitHub Desktop.
Save DanyelMorales/da42cbc0dab10bcdf4b7eefa9208565c to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class PascalTriangle
{
public static void main(String a[]) throws Exception
{
int num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter number of rows for pascal triangle:");
num=scan.nextInt();
System.out.print("Pascal Triangle of "+num+" is:\n");
//Write your logic here
int[][] container = new int[num][num];
for(int i=0; i<num; i++){
container[i][0] = 1;
System.out.print(new String(new char[((num-1)-i)]).replace("\00","_"));
System.out.print("1_");
for(int y=1; y < i; y++){
int aaa = (container[i-1][y-1] + container[i-1][y]);
container[i][y]=aaa;
System.out.print(aaa+"_");
}
container[i][i] = 1;
System.out.println(i>0?"1_":"");
}
//end
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment