Wednesday, March 9, 2016

Print all the Fibonacci numbers less than value n

Code:

using System;

public class Test
{
    public static void Main()
    {
        int n = 13;

        Console.WriteLine("Input: " + n);

        Console.WriteLine("Output: ");

        new Test().Fibonacci(n);
    }

    public void Fibonacci(int n)
    {
        int prevprev = 0;
        int prev = 1;

    Console.WriteLine(prevprev);

        while(prev <= n)
        {
        Console.WriteLine(prev);
        
        prev = prev + prevprev;
            prevprev = prev - prevprev;
        }
    }

}

Output:

Input: 13
Output: 
0
1
1
2
3
5
8
13

No comments:

Post a Comment