Java Code to Print Pascal Triangle - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, September 25, 2017

Java Code to Print Pascal Triangle

Write a Java Code to Print Pascal Triangle

Program:
/* 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();
}
}
}


Output:
Enter Number of Rows : 5
     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 

No comments:

Post a Comment