In this article, we will understand how to print star Pascal’s triangle. The Pascal’s triangle is formed by using multiple for-loops and print statements. All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in Pascal’s triangle, 0s are invisible. The second row is acquired by adding (0+1) and (1+0). The output is sandwiched between two zeroes. The process continues till the required level is achieved.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the number of rows in Pascal's Triangle : 8
Output
The desired output would be −
Suppose our input is −
Enter the number of rows in Pascal's Triangle : 8 The Pascal's Triangle : 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1
Algorithm
Step 1 - START Step 2 - Declare three integer values namely i, j and my_input Step 3 - Read the required values from the user/ define the values Step 4 - Define a function ‘factorial()’ to compute the factorial of two numbers and a function ‘Combination()’ to compute combination of two numbers Step 5 - We iterate through two nested 'for' loops to get space between the characters. Step 6 - After iterating through the innermost loop, we iterate through another 'for' loop. This will help Combination values of ‘i’ and ‘j’. Step 7 - Now, print a newline to get the specific number of Combination values of ‘i’ and ‘j’ in the subsequent lines. Step 8 - Display the result Step 9 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class PascalsTriangle { static int factorial(int my_input) { int factors; for(factors = 1; my_input > 1; my_input--){ factors *= my_input; } return factors; } static int combination(int my_input,int r) { return factorial(my_input) / ( factorial(my_input-r) * factorial(r) ); } public static void main(String args[]){ System.out.println(); int my_input, i, j; my_input = 5; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number of rows in Pascal's Triangle : "); my_input = my_scanner.nextInt(); System.out.println("The Pascal's Triangle : "); for(i = 0; i <= my_input; i++) { for(j = 0; j <= my_input-i; j++){ System.out.print(" "); } for(j = 0; j <= i; j++){ System.out.print(" "+combination(i, j)); } System.out.println(); } } }
Output
Required packages have been imported A reader object has been defined Enter the number of rows in Pascal's Triangle : 8 The Pascal's Triangle : 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class PascalsTriangle { static int factorial(int my_input) { int factors; for(factors = 1; my_input > 1; my_input--){ factors *= my_input; } return factors; } static int combination(int my_input,int r) { return factorial(my_input) / ( factorial(my_input-r) * factorial(r) ); } public static void main(String args[]){ System.out.println(); int my_input, i, j; my_input = 8; System.out.println("The number of rows in Pascal's Triangle is defined as " +my_input); System.out.println("The Pascal's Triangle : "); for(i = 0; i <= my_input; i++) { for(j = 0; j <= my_input-i; j++){ System.out.print(" "); } for(j = 0; j <= i; j++){ System.out.print(" "+combination(i, j)); } System.out.println(); } } }
Output
The number of rows in Pascal's Triangle is defined as 8 The Pascal's Triangle : 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1