TheBulgarianWolf

Tribonacci Sequence

Nov 2nd, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MoreMethodExercises
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Enter the numbers you want to get from a tribonacci sequence: ");
  10.             int num = int.Parse(Console.ReadLine());
  11.             TribonacciSeq(num);
  12.         }
  13.  
  14.         static void TribonacciSeq(int number)
  15.         {
  16.             int[] arr = new int[number];
  17.             arr[0] = 1;
  18.             arr[1] = 1;
  19.             arr[2] = 2;
  20.             if(number == 1)
  21.             {
  22.                 Console.WriteLine("1");
  23.             }
  24.             else if(number == 2)
  25.             {
  26.                 Console.WriteLine("1 1");
  27.             }
  28.             else if (number == 3)
  29.             {
  30.                 Console.WriteLine("1 1 2");
  31.             }
  32.             else if(number > 3)
  33.             {
  34.                 for (int k = 3; k < arr.Length; k++)
  35.                 {
  36.                     arr[k] = arr[k - 3] + arr[k - 2] + arr[k - 1];
  37.                 }
  38.                 foreach (int m in arr)
  39.                 {
  40.                     Console.Write(m + " ");
  41.                 }
  42.             }
  43.             else
  44.             {
  45.                 Console.WriteLine("Incorrect input");
  46.             }
  47.            
  48.            
  49.         }
  50.  
  51.     }
  52. }
  53.  
Add Comment
Please, Sign In to add comment