AF
Home
Tag
Submit Notes
Ask Anything
Login
Subscribe Us
A
ny
F
orum
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.
Follow @anyforumin
corejava-programming: Help Pascal for constructing his Triangle Pattern
Pascal has forgotten the algorithm for constructing his triangle.
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Can you help him to construct the triangle?
Sample Input
5
Sample Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
corejava x 353
programming x 169
Posted On :
2018-01-28 17:16:34.0
Divesh
101
18
0
5
Answers
import java.util.Scanner;
public class NumberTriangle{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n,temp=1;
n=sc.nextInt();
for(int i=0;i<n;i++){
for(int k=n-1;k>i;k--){
System.out.print(" ");
}
temp=1;
for(int j=0;j<=i;j++){
if(j==i){
System.out.print(temp);
}else{
System.out.print(temp+" ");
temp=temp*(i-j)/(j+1);
}
}
System.out.println();
}
}
}
Posted On :
2018-01-28 18:33:45
Satisfied :
4 Yes
0 No
Ananya
4
10
0
Reply This Thread
5
Post Answer
Please Login First to Post Answer:
Login
Answer:
anyforum.in