RGAlex

O4. Tribonacci Sequence / Methods - More Exercise

Feb 9th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. package MethodsMoreExercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class O4TribonacciSequence {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int number = Integer.parseInt(scanner.nextLine());
  9.         printTribonacciLine(number);
  10.     }
  11.  
  12.     private static void printTribonacciLine(int number) {
  13.         int[] newArray = new int[number];
  14.         for (int i = 0; i < number; i++) {
  15.             int sum = 0;
  16.             if (i == 0 || i == 1) {
  17.                 sum = 1;
  18.  
  19.             } else if (i == 2) {
  20.                 sum = 2;
  21.  
  22.             } else {
  23.                 sum = newArray[i - 1] + newArray[i - 2] + newArray[i - 3];
  24.  
  25.             }
  26.             newArray[i] = sum;
  27.  
  28.             System.out.print(sum + " ");
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment