Advertisement
Layvu

Dp1

Apr 24th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. using System;
  2. using System.Reflection.Emit;
  3.  
  4. namespace ConsoleApplication1945
  5. {
  6.     internal class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             Console.WriteLine(Fib(10));
  11.         }
  12.  
  13.         public static int Fib(int number)
  14.         {
  15.             if (number < 0) throw new ArgumentException();
  16.            
  17.             var first = 0;
  18.             var second = 1;
  19.            
  20.             if (number == 0) return first;
  21.             if (number == 1) return second;
  22.  
  23.             for (var i = 2; i <= number; i++)
  24.             {
  25.                 var temp = first + second;
  26.                 first = second;
  27.                 second = temp;
  28.             }
  29.  
  30.             return second;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement