Home » Java programs » Java basic programs

Java program to print Pascal's triangle



In this java program, we are going to read number of rows and printing Pascal’s triangle according to the input.
Submitted by IncludeHelp, on November 07, 2017

Here, we are printing the Pascal’s triangle based upon given number of rows.

Example:

Input:
Enter number of rows: 10

Output:
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
                   1      9      36      84      126      126      84      36      9  

Program to print Pascal's triangle in java

import java.util.Scanner;

public class Pattern13
{
	public static void main(String[] args) 
	{
		// initialize variables.
		int lib,p,q,r,x;

		// create object of scanner.
		Scanner s=new Scanner(System.in);

		// enter number of rows.
		System.out.print("Enter the rows : ");
		r=s.nextInt();
		lib=1;  
		q=0;

		// here the pascals triangle.
		System.out.println("Pascal's Triangle : ");

		while(q<r)
		{
			for(p=40-3*q;p>0;--p)
			System.out.print(" ");
			for(x=0;x<=q;++x)
			{
				if((x==0)||(q==0))
					lib=1;
				else
					lib=(lib*(q-x+1))/x;
				System.out.print("      ");
				System.out.print(lib);
			}
			System.out.println("");
			++q;
		}
	}
}

Output

Enter the rows : 10
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
                   1      9      36      84      126      126      84      36      9  





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.