codescracker


java

Java Program to Print Pascal Triangle



« Previous Program Next Program »


Print Pascal Triangle

To print pascal triangle in Java Programming, you have to use three for loops and start printing pascal triangle as shown in the following example.

Java Programming Code to Print Pascal Triangle

Following Java Program ask to the user to enter the number of line/row upto which the Pascal triangle will be printed to print the Pascal triangle on the screen:

/* Java Program Example - Print Pascal Triangle */
		
import java.util.Scanner;

public class JavaPascalTriangle
{
    public static void main(String args[])
	{
	    int r, i, k, number=1, j;
		Scanner scan = new Scanner(System.in);
		
		System.out.print("Enter Number of Rows : ");
		r = scan.nextInt();
		
		for(i=0;i<r;i++)
		{
			for(k=r; k>i; k--)
			{
				System.out.print(" ");
			}
            number = 1;
			for(j=0;j<=i;j++)
			{
				 System.out.print(number+ " ");
                 number = number * (i - j) / (j + 1);
			}
			System.out.println();
		}
	}
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program print pascal triangle

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test