原文:

打印。第一个斐波那契数列是:

c

// simple cpp program to print fibonacci
// sequence
#include 
using std::cout;
void fib(int n)
{
    int a = 0, b = 1, c;
    if (n >= 0)
        cout << a << " ";
    if (n >= 1)
        cout << b << " ";
    for (int i = 2; i <= n; i  ) {
        c = a   b;
        cout << c << " ";
        a = b;
        b = c;
    }
}
// driver code
int main()
{
    fib(9);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// simple java program to print
// fibonacci  sequence
import java.io.*;
class gfg {
static void fib(int n)
{
    int a = 0, b = 1, c;
    if (n >= 0)
        system.out.print( a   " ");
    if (n >= 1)
        system.out.print( b   " ");
    for (int i = 2; i <= n; i  )
    {
        c = a   b;
        system.out.print( c   " ");
        a = b;
        b = c;
    }
}
    // driver code
    public static void main (string[] args)
    {
            fib(9);
    }
}
// this code is contributed by anuj_67.

python 3

# simple python3 program to
# print fibonacci sequence
def fib(n):
    a = 0
    b = 1
    if (n >= 0):
        print(a, end=' ')
    if (n >= 1):
        print(b, end=' ')
    for i in range(2, n 1):
        c = a   b
        print(c, end=' ')
        a = b
        b = c
# driver code
fib(9)
# this code is contributed
# by prasad kshirsagar

c

// simple c# program to print
// fibonacci sequence
using system;
class gfg
{
static void fib(int n)
{
    int a = 0, b = 1, c;
    if (n >= 0)
        console.write( a   " ");
    if (n >= 1)
        console.write( b   " ");
    for (int i = 2; i <= n; i  )
    {
        c = a   b;
        console.write( c   " ");
        a = b;
        b = c;
    }
}
// driver code
public static void main ()
{
        fib(9);
}
}
// this code is contributed by anuj_67.

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 0)
        echo $a , " ";
    if ($n >= 1)
        echo $b , " ";
    for ( $i = 2; $i <= $n; $i  )
    {
        $c = $a   $b;
        echo $c , " ";
        $a = $b;
        $b = $c;
    }
}
// driver code
fib(9);
// this code is contributed by anuj_67.
?>

java 描述语言


output: 

0 1 1 2 3 5 8 13 21 34