Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Make the program in c# to determine "n" numbers of fibonacci using 3 variables

0 votes
asked Apr 13, 2019 by Manuelito Del Angel (130 points)
Input:

7

Output

1,1,2,3,5,8,12

2 Answers

0 votes
answered Mar 31, 2021 by GD rares5750 (140 points)

If you only want 3 variables to be used to make the fibonacci sequence happen, then the could should be:

          int num1 = 0;
            int num2 = 0;
            int num3 = 1;
            Console.Write("How many numbers of the fibbonacci sequence do you want printed out?: ");
            int amountOfNumPrint = Convert.ToInt32(Console.ReadLine());
            for(int i = 0; i < amountOfNumPrint; i++)
            {
                if (num3 < 0)
                {
                    Console.WriteLine("We cannot go past the number above.");
                    i = amountOfNumPrint + 15;
                    continue;
                }
                Console.WriteLine(num3);
                num1 = num2;
                num2 = num3;
                num3 = num1 + num2;
            }

But if it is 3 variables in general, then I am not experienced enough to do that, and will let the rest of the community battle out.

commented Apr 1, 2021 by KKN (1,110 points)
Wait.. ANOTHER GEOMETRY DASH PLAYER!? :OO
commented Apr 5, 2021 by VASU THOTA (100 points)

#include<stdio.h>

main()

{

int n;

printf("enter the number,such that upto you want the fibonacci series :\n");

scanf("%d",&n);

int x,y,z;

x=0;

y=1;

z=0;

printf("the required fabonacci_series is :\n");

while (z<=n)

{

printf("%d \n",z);

x=y;

y=z;

z=x+y;

}

}

commented Apr 7, 2021 by Peter Minarik (84,720 points)
edited Apr 7, 2021 by Peter Minarik
One slight mistake there: your Fibonacci sequence starts with 1, 1, 2, ...; while it should start with 0, 1, 1, 2, ...

Please, see https://en.wikipedia.org/wiki/Fibonacci_number
0 votes
answered Apr 7, 2021 by Peter Minarik (84,720 points)
edited Apr 7, 2021 by Peter Minarik

Below is my solution to the problem.

Some notes:

  • The code is commented for more clarity.
  • C# actually supports arbitrarily large integral numbers: System.Numerics.BigInteger. Unfortunately, OnlineGDB doesn't seem to have this library enabled.
  • Since System.Numerics.BigInteger is not supported, the code checks for overflow and stops when it couldn't process any more numbers on 64 bit integral arithmetics.

Enjoy:

using System;

// Note: OnlineGDB does not seem to support it, but there is a type specifically designed to handle arbitrarily large integers: System.Numerics.BigInteger
internal class Fibonacci
{
    // The last two elements in the sequence
    private ulong a = 0; // The 1st element of the sequence is 0
    private ulong b = 1; // The 2nd element of the sequence is 1
    
    // Print a number on the screen
    private void Print(uint index, ulong number) => Console.WriteLine($"F({index}):\t{number}");
    
    // Print and iterate to the next element in the Fibonacci sequence
    private bool Next(uint index)
    {
        try
        {
            ulong c = checked(a + b);
            Print(index, c);
            a = b;
            b = c;
            return true;
        }
        catch (OverflowException)
        {
            Console.WriteLine("Unfortunately no larger number can be processed on 64 bit arithmetic types.");
            return false;
        }
    }
    
    // Show n number of elements from the Fibonacci sequence
    public void Show(uint n)
    {
        Console.WriteLine($"The first {n} Fibonacci number{(n != 1 ? "s are" : " is")}:");
        
        // The first 2 element of the Fibonacci seuqence is non-computable
        if (n > 0)
            Print(0, a);
        if (n > 1)
            Print(1, b);
        
        // From element 2, the sequence is computable
        for (uint i = 2; i < n && Next(i); i++)
            ; // We have already processed the next Fibonacci number in Next() in the condition of `for`
    }
}

public class Program
{
    // Get a positive integral number from the user.
    static uint GetLimit()
    {
        do
        {
            Console.WriteLine("How many Fibonacci numbers to display?");
            Console.Write(": ");
        } while (!UInt32.TryParse(Console.ReadLine(), out var limit) || limit == 0);
        
        Console.WriteLine();
        return limit;
    }
    
    static void Main()
    {
        Fibonacci fibonacci = new Fibonacci();
        fibonacci.Show(GetLimit());
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...