0
how to find Fibonacci series using c program
2 ответов
+ 1
http://code.sololearn.com/cmrMVI4Eikwm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static int fibonacci_rec(int i, int curr, int prev) {
if (i == 0) {return prev;}
if (i == 1) {return curr;}
return fibonacci_rec(i-1, curr+prev, curr);
}
static int fibonacci(int i) {
return fibonacci_rec(i, 1, 0);
}
static void Main(string[] args)
{
int n;
Console.WriteLine("Fibonacci");
Console.Write("Please enter a number: ");
n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(n);
Console.WriteLine("fib(" + n + ") = " + fibonacci(n));
}
}
}
0
Refer to Methods -> Recursion, turn on your imagination, find an answer :)