AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
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 ?.



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 352
programming x 168
Posted On : 2018-01-28 17:16:34.0
profile Divesh - anyforum.in Divesh
101180
up-rate
5
down-rate

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
profile Ananya - anyforum.in Ananya
4100
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in