MiroslavKisov

StackFibonacci

Jan 25th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace StackFibonacci
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main()
  9.         {
  10.             var N = int.Parse(Console.ReadLine());
  11.             var stack = new Stack<long>();
  12.             long firstFibo = 0;
  13.             long secondFibo = 1;
  14.             long nthFibo = 0;
  15.             stack.Push(secondFibo);
  16.             stack.Push(firstFibo);
  17.             for (int i = 0; i < N - 1; i++)
  18.             {
  19.                 firstFibo = stack.Pop();
  20.                 secondFibo = stack.Pop();
  21.                 nthFibo = firstFibo + secondFibo;
  22.                 stack.Push(nthFibo);
  23.                 stack.Push(secondFibo);
  24.             }
  25.             Console.WriteLine(nthFibo);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment