Advertisement
csaki

algo2 09.27 (fakt, fibonacci)

Sep 17th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace algoHazi_09_17
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine(fakt(10));
  13.             Console.WriteLine(fibonacci(10));
  14.             Console.ReadLine();
  15.         }
  16.  
  17.         static int fakt(int k)
  18.         {
  19.             if (k == 0) return 1;
  20.             else
  21.             {
  22.                 int f = 1;
  23.                 for (int i = 1; i <= k; i++)
  24.                 {
  25.                     f *= i;
  26.                 }
  27.                 return f;
  28.             }
  29.         }
  30.  
  31.         static int fibonacci(int n)
  32.         {
  33.             if (n <= 1) return n;
  34.             else
  35.             {
  36.                 int a = n - 1;
  37.                 int b = n - 2;
  38.                 int c = a + b;
  39.  
  40.                 for (int i = 3; i <= n; i++)
  41.                 {
  42.                     Console.WriteLine(c);
  43.                     a = n - i;
  44.                     b = n - 1 - i;
  45.                     c += a + b;
  46.                 }
  47.                 return c;
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement