Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- class CalcFib {
- static void Main(String[] args)
- {
- // Set n equal to an integer taken from user input
- int n = Convert.ToInt32(Console.ReadLine());
- // Output the calulated fib number
- Console.WriteLine(Fibonacci(n));
- }
- public static int Fibonacci(int n)
- {
- // Declare integer variables to be used in the fibonacci sequence
- int a = 0, b = 1, c = 0;
- // To return the first Fibonacci number
- if (n == 0) return a;
- // Run a for loop to calulate all fibonacci numbers after 0
- for (int i = 2; i <= n; i++)
- {
- // New fib number is equal to sum of past 2 numbers
- c = a + b;
- a = b;
- b = c;
- }
- // Return the calculated fib number
- return b;
- }
- }
Add Comment
Please, Sign In to add comment