pascal triangle in java

Learn Java Programming for Beginners

Pascal triangle is the mathematical concept in which numbers are arrange in triangular manner.image below shoes how pascal triangle is formed

PascalTriangle

PascalTriangle

Program to display pascal triangle in java
import java.io.*;

class pascal
{
public static void main(String args[])throws IOException
{
int m=0,x,b,rows=0;
DataInputStream in=new DataInputStream(System.in);
rows=Integer.parseInt(in.readLine());
do
{
for(int j=rows;j>m;j--)
{

System.out.print(" ");
}
x=0;b=1;
while(x<=m)

{
if(m==0||x==0)
System.out.print(" "+b);
else
{
b=b*(m-x+1)/x;
System.out.print(" "+b);
}
x=x+1;
}
m=m+1;
System.out.println();
}
while(m<=rows);
}
}

output:

pascal triangle in java

pascal triangle in java

Pascal Triangle In C 

Facing difficulties in understanding the program ?

Ask Your Queries in Comment Box

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *