Monday, April 16, 2007

Pascal Triangle

import javax.swing.*;

public class Pascal{
public static void main(String[] args){
Pascal pas=new Pascal();
}
public Pascal(){
String output="",input;
int num,a,b;

input=JOptionPane.showInputDialog("Enter a number:");
num=Integer.parseInt(input);

for (int x=1;x<=num;x++)
{
for (int z=num;z>=x;z--)
{
output+=" ";
}
output+="1"+" ";
b=1;a=0;
for(int y=x;y>1;y--)
{
b=(y*b)/(a+1);
output+=b+" ";
a++;
}
output+="1";
output+="\n";
}




JOptionPane.showMessageDialog(null,output,"PASCAL TRIANGLE",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}

No comments: