Created
July 5, 2020 17:57
-
-
Save DanyelMorales/da42cbc0dab10bcdf4b7eefa9208565c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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