Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 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.  
  8.  
  9.  
  10. namespace Zad8
  11. {
  12. /*
  13. Napisz program obliczający n-ty wyraz ciągu Fibbonaciego:
  14. 1.Rekurencyjnie
  15. 2.Iteracyjnie
  16. */
  17.  
  18. class Fib
  19. {
  20. //rekurencyjnie
  21.  
  22. public static int fibr(int n)
  23. {
  24. if ((n == 1) || (n == 2))
  25. return 1;
  26. else
  27. return fibr(n - 1) + fibr(n - 2);
  28. }
  29. public static int fibi(int n)
  30. {
  31. int a, b;
  32. if (n == 0) return 0;
  33.  
  34. a = 0; b = 1;
  35. for (int i = 0; i < (n - 1); i++)
  36. {
  37. swap(a, b);
  38. b += a;
  39. }
  40. return b;
  41. }
  42.  
  43.  
  44.  
  45.  
  46. static void Main(string[] args)
  47. {
  48. String line;
  49. int n;
  50.  
  51. Console.WriteLine("Podaj, ktory wyraz ciagu Fibonacciego obliczyc");
  52. line = Console.ReadLine();
  53. n = int.Parse(line);
  54.  
  55. Console.WriteLine(n + "-ty wyraz ciagu Fibonacciego rek: " + fibr(n));
  56. Console.WriteLine(n + "-ty wyraz ciagu Fibonacciego iter: " + fibi(n));
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement