Advertisement
braveheart1989

FibonacciNumbers

May 23rd, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _22.FibonacciNumbers
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             if (n==1 || n == 0)
  15.             {
  16.                 Console.WriteLine(1);
  17.                 return;
  18.             }
  19.            
  20.             Console.WriteLine(Fib(n));
  21.  
  22.         }
  23.  
  24.         static int Fib(int n)
  25.         {
  26.             int x1 = 1;
  27.             int x2 = 1;
  28.             int sum = 0;
  29.  
  30.             for (int i = 2; i <= n; i++)
  31.             {
  32.                 sum = x1 + x2;
  33.                 x1 = x2;
  34.                 x2 = sum;
  35.             }
  36.             return sum;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement