CODE
import java.io.*;
import java.util.*;
public class Pascal {
public static void main(String[] args) {
int rows;
Stack<Integer> s1 = new Stack<Integer>();
Stack<Integer> s2 = new Stack<Integer>();
s1.push(1);
s1.push(1);
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows:");
rows = in.nextInt();
System.out.println("Pascal Traingle:");
for(int i = 1; i <= rows; i++) {
if(i==1 || i==2) {
if(i==1)
System.out.println("1");
else {
System.out.println();
System.out.println("1 1");
}
}
else {
System.out.println();
int p = s1.pop();
System.out.print(p+" ");
s2.push(p);
while(s1.empty() == false) {
int q = s1.pop();
int next = p+q;
System.out.print(next+" ");
s2.push(next);
p = q;
}
System.out.println(" 1 ");
s2.push(1);
while(s2.empty() == false) {
s1.push(s2.pop());
}
}
} // end of i loop
}
}
No comments:
Post a Comment