Guest User

Untitled

a guest
May 20th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. class CalcFib {
  6.  
  7. static void Main(String[] args)
  8. {
  9. // Set n equal to an integer taken from user input
  10. int n = Convert.ToInt32(Console.ReadLine());
  11.  
  12. // Output the calulated fib number
  13. Console.WriteLine(Fibonacci(n));
  14. }
  15.  
  16. public static int Fibonacci(int n)
  17. {
  18. // Declare integer variables to be used in the fibonacci sequence
  19. int a = 0, b = 1, c = 0;
  20.  
  21. // To return the first Fibonacci number
  22. if (n == 0) return a;
  23.  
  24. // Run a for loop to calulate all fibonacci numbers after 0
  25. for (int i = 2; i <= n; i++)
  26. {
  27. // New fib number is equal to sum of past 2 numbers
  28. c = a + b;
  29. a = b;
  30. b = c;
  31. }
  32.  
  33. // Return the calculated fib number
  34. return b;
  35. }
  36. }
Add Comment
Please, Sign In to add comment