How to print Pascal Triangle in Java - Example Tutorial (original) (raw)

Printing patterns with stars or numbers and triangles are some of the common programming exercises. Earlier we have seen how to print pyramid patterns with stars and today you will learn how to print Pascal's triangle in Java. Sometimes this problem is also asked as "write a program to print Pascal triangle without using array" or by just using for loop. Pascal’s triangle is a set of numbers arranged in the form of a triangle, similar to Floyd's triangle but their shape is different. Each number in the Pascal triangle row is the sum of the left number and a right number of the previous row. If a number is missing in the above row, it is assumed to be 0. The first row starts with number 1, that's why you will see that the first two rows of the Pascal triangle just contain 1.

Here is Pascal's triangle with 6 rows, you can see it's not the number but the formatting which is difficult to code.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

The triangle is named after the famous french mathematician Blaise Pascal who organized detailed information on the triangle in a book. However, this triangle was already known to many ancient civilizations.

Pascal’s triangle has many unique properties e.g. the sum of numbers in each row is twice the sum of numbers in the above row and the diagonals adjacent to the border diagonals contain natural numbers in order.

Pascal triangle is also related to the Fibonacci series, if you add the numbers in Pascal's triangle in diagonal lines going up, you get one of the Fibonacci numbers. Even though the post is about printing Pascal's triangle but a bit of history always helps.

Printing Pascal Triangle in Java

Here is the Java program to print Pascal's triangle without using an array. I have encapsulated logic inside a static method so that I can directly call it from the main method, as you might know, that you can only call the static method from the main in Java.

The method has two loops because we are printing two-dimensional patterns. The outer loop prints number of rows in the Pascal triangle and the inner loop is responsible for printing numbers in each row. The complexity of this solution is O(n^2) where n is the number of rows.

You should also pay some attention to the formatting commands we have used above to create a nicely formatted triangle. The %4d formatting instruction is used to print the number within 4 spaces. We chose 4 since we know the maximum number of digits in the largest number of a Pascal triangle with 10 rows is 3 digits.

Btw, if you want more coding problems for practice, you should check Cracking the Coding Interview, which contains more than 189 coding problems from technology companies like Google, Amazon, Facebook, Microsoft, ThoughtWorks, Apple, Twitter, and several other startups.

Java Program to print pascal triangle

If you are more interested in learning algorithms, then you should read a good book on data structure and algorithms like Introduction to Algorithm by Thomas Cormen.

Now, here is our sample program in Java to print Pascal's triangle for a given number of rows. It accepts the number of rows from the user via the command prompt.

import java.util.Scanner;

/*

}

Output Welcome to Java program to print Pascal's triangle Please enter number of rows of Pascal's triangle 4 Pascal's triangle with 4 rows 1 1 1 1 2 1 1 3 3 1

Welcome to Java program to print Pascal's triangle Please enter number of rows of Pascal's triangle 7 Pascal's triangle with 7 rows 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

That's all about how to print Pascal's triangle in Java. As I said, it's not a difficult problem, the logic to generate numbers in each row is simple, each number is the sum of a number of its left and right in the previous row. The only tricky part is to properly format the output. In order to do that, you must know the maximum digit in the maximum number in the Pascal triangle you are printing.

Alternatively, you can also solve the following coding problems from this blog:

This problem assumes that it will only print Pascal triangle up to 10 rows. If you want to practice more problems, see Cracking the Coding Interview book.

Thanks for reading this article. If you like then please share it with your friends and colleagues. If you have any questions or feedback, please drop a note. If you have any questions or doubt then please let us know and I'll try to find an answer for you. Btw, what is your favorite coding exercise? prime number, palindrome or this one?