Jump to content

[C#] Generarea unui triunghi cu șirul lui Fibonacci


Clanin3
 Share

Recommended Posts

[!] Sirul lui Fibonacci este un sir infinit de numere, care au la baza o formula simpla: n = n[i-1] + n[i-2]. Pe baza acestei formule se genereaza elementele sirului.

 

using System;

namespace fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0, b = 1, c, n; //initializari
            Console.Write("Introdu lungimea: ");
            n = Int32.Parse(Console.ReadLine()); //citirea lungimii triunghiului
            for(int i = 1; i<=n; i++)
            {
                a = 0; b = 1; //reinceperea sirului
                Console.Write(b + " "); //afisarea primului element din sir
                for(int j = 1; j<i; j++)
                {
                    c = a + b; // formarea urmatorului element din sir din suma ultimelor 2 valori
                    Console.Write(c + " ");
                    a = b;
                    b = c; //actualizarea valorilor cu valoarea precedenta, respectiv cu valoarea actuala.
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

 

Output:

 

image.png

Link to comment
Share on other sites

In this section we post tutorials, not creations.

You have to explain what you have done so that each member reading your topic understands your snippet.

Link to comment
Share on other sites

It's not a creation, but an algorithm. If you see, I explained all steps in comments using '//'. What do you want me to explain more, how to use the 'foor loop'?

Not all tutorials are based on simple things, like "vectors and arrays" or any other base things. A tutorial could be an algorithm, or at school you learn only how to use a for loop, conditionals (if, switch, "?"), or how to use OOP in your creation? Don't learn you algorithms?

Link to comment
Share on other sites

12 hours ago, Clanin3 said:

A tutorial could be an algorithm

Algorithm is algorithm, a script is a script don't mess with definitions.

12 hours ago, Clanin3 said:

or at school you learn only how to use a for loop, conditionals (if, switch, "?"), or how to use OOP in your creation? Don't learn you algorithms?

Yes we learn what's mentioned by you, and also we learn the drawing algorithms.

 What I expected is a better explanation for the utility of Fibonacci sequence, is it just used to draw? Do you have a better ideas where we use it?

Is it used in famous games or programs ? That's what we expect from a tutorial. Please accept critics, I used a decent language/way and I expect the same.

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.