VelizarAvramov

05. Fibonacci Numbers

Nov 17th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _05._Fibonacci_Numbers
  4. {
  5.     class FibonacciNumbers
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.             int result = FindFibonacciNumbers(n);
  11.             Console.WriteLine(result);
  12.         }
  13.  
  14.         private static int FindFibonacciNumbers(int n)
  15.         {
  16.             int result = 0;
  17.             if (n == 1 || n == 0)
  18.             {
  19.                 return 1;
  20.             }
  21.             else
  22.             {
  23.                 int before = 1;
  24.                 int beforePrevious = 1;
  25.                 for (int i = 2; i <= n; i++)
  26.                 {
  27.                     result = before + beforePrevious;
  28.                     beforePrevious = before;
  29.                     before = result;
  30.                 }
  31.                 return result;
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment