# Write a program to print pascal triangle in java

Let's say that we have computed the fourth row - 1 3 3 1. So, the complete fifth row would be 1 4 6 4 1. At any iteration, I represents the row number that we are printing.

### Reverse pascal triangle in java

Given below is a complete program which takes an input n from the user and prints the first n lines of the Pascal's triangle. The only tricky part is to properly format the output. Let's say that we have computed the fourth row - 1 3 3 1. So, the complete fifth row would be 1 4 6 4 1. When the inner loop exits, currentRow would be populated. So, in order to find the numbers in the nth row of the triangle, we need the values of the n-1 the row. Even though the post is about printing the Pascal's triangle but a bit history always helps. Pascal triangle is also related to Fibonacci series , if you add the numbers in Pascal's triangle in diagonal lines going up, you get one of the Fibonacci numbers. At any iteration, I represents the row number that we are printing. In order to do that, you must know the maximum digit in the maximum number in the Pascal triangle you are printing. We chosen 4 since we know the maximum number of digits in the largest number of a Pascal triangle with 10 rows is 3 digits. The values stored in currentRow are printed and previousRow is assigned with currentRow so that it can be used in the next iteration of the outer loop. The remaining numbers are obtained by summing the two numbers that lie directly above that number on the previous row and the number that follows it.

Pascal triangle is also related to Fibonacci seriesif you add the numbers in Pascal's triangle in diagonal lines going up, you get one of the Fibonacci numbers. We maintain two int arrays, named currentRow and previousRow.

The first and the last element would be one. I have encapsulated logic inside a static method so that I can directly call it from main method, as you might know that you can only call static method from main in Java. The values stored in currentRow are printed and previousRow is assigned with currentRow so that it can be used in the next iteration of the outer loop.

### Explain logic of pascal triangle in c

Pascal's triangle using loops We will first see how to generate a unformatted Pascal's triangle using simple looping statements. When the inner loop exits, currentRow would be populated. Given below is a complete program which takes an input n from the user and prints the first n lines of the Pascal's triangle. We maintain two int arrays, named currentRow and previousRow. Even though the post is about printing the Pascal's triangle but a bit history always helps. In order to do that, you must know the maximum digit in the maximum number in the Pascal triangle you are printing. We chosen 4 since we know the maximum number of digits in the largest number of a Pascal triangle with 10 rows is 3 digits. The currentRow will be initialised with an array of i the loop counter elements with the first and the last element of the array set to '1'. It accept the number of rows from user via command prompt. The first and the last element would be one. Further Learning.

Rated 10/10
based on 105 review

Download