Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Zad8
- {
- /*
- Napisz program obliczający n-ty wyraz ciągu Fibbonaciego:
- 1.Rekurencyjnie
- 2.Iteracyjnie
- */
- class Fib
- {
- //rekurencyjnie
- public static int fibr(int n)
- {
- if ((n == 1) || (n == 2))
- return 1;
- else
- return fibr(n - 1) + fibr(n - 2);
- }
- public static int fibi(int n)
- {
- int a, b;
- if (n == 0) return 0;
- a = 0; b = 1;
- for (int i = 0; i < (n - 1); i++)
- {
- swap(a, b);
- b += a;
- }
- return b;
- }
- static void Main(string[] args)
- {
- String line;
- int n;
- Console.WriteLine("Podaj, ktory wyraz ciagu Fibonacciego obliczyc");
- line = Console.ReadLine();
- n = int.Parse(line);
- Console.WriteLine(n + "-ty wyraz ciagu Fibonacciego rek: " + fibr(n));
- Console.WriteLine(n + "-ty wyraz ciagu Fibonacciego iter: " + fibi(n));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement