Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //input number of lines
- int n = scanner.nextInt();
- //save the number of lines temporary
- int temp = n;
- //create array that will be printed
- int[] arr = new int[n];
- //that array will save the main one
- int[] arrTemp = new int[n];
- n = 0;
- while(n<=temp){
- //lets loop through the array
- for(int i = 0; i <n; i++){
- //if the number is in first or last place it is 1
- if(i == 0 || i == n-1){
- arr[i] = 1;
- //arrTemp[i] = 1;
- }
- //otherwise, the number would be the sum of the number above it and the number ot its left
- else{
- arr[i] = arrTemp[i-1] + arrTemp[i];
- }
- // print the next line of numbers
- System.out.print(arr[i] + " ");
- }
- //now save the current line to the temp line, that would be useful when we want to
- //sum the numbers in the next line
- for(int i = 0; i <n; i++){
- arrTemp[i] = arr[i];
- }
- // count of numbers per line increases
- n++;
- //space the lines
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement