📜  使用2个变量打印斐波那契数列

📅  最后修改于: 2021-04-24 04:24:30             🧑  作者: Mango

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

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.


Python3
# 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.


PHP
= 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.
?>


C++
// CPP Program to print Fibonacci sequence
// using 2 variables
#include 
using std::cout;
void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        cout << a << " ";
    if (n >= 1)
        cout << b << " ";
    for (int i = 2; i <= n; i++) {
        cout << a + b << " ";
        b = a + b;
        a = b - a;
    }
}
  
// Driver code
int main()
{
    fib(9);
    return 0;
}


Java
// Java Program to print 
// Fibonacci sequence
// using 2 variables
import java.io.*;
class GFG {
      
static void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        System.out.print(a + " ");
    if (n >= 1)
        System.out.print(b + " ");
    for (int i = 2; i <= n; i++) 
    {
        System.out.print(a + b + " ");
        b = a + b;
        a = b - a;
    }
}
  
    // Driver code
    public static void main (String[] args) 
    {
        fib(9);
    }
}
  
// This code is contributed by anuj_67.


Python3
# Simple Python3 Program to
# print Fibonacci sequence
# using 2 variables
  
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):
        print(a + b, end=' ')
        b = a + b
        a = b - a
  
# Driver code
fib(9)
  
# This code is contributed by
# Prasad Kshirsagar


C#
// C# Program to print Fibonacci
// sequence using 2 variables
using System;
  
class GFG {
          
    static void fib(int n)
    {
        int a = 0, b = 1;
          
        if (n >= 0)
            Console.Write(a + " ");
        if (n >= 1)
            Console.Write(b + " ");
        for (int i = 2; i <= n; i++) 
        {
            Console.Write(a + b + " ");
            b = a + b;
            a = b - a;
        }
    }
  
    // Driver code
    public static void Main () 
    {
        fib(9);
    }
}
  
// This code is contributed by anuj_67.


PHP
= 0)
        echo $a , " ";
    if ($n >= 1)
        echo $b , " ";
    for ($i = 2; $i <= $n; $i++)
    {
        echo $a + $b , " ";
        $b = $a + $b;
        $a = $b - $a;
    }
}
  
    // Driver code
    fib(9);
      
// This code is contributed by anuj_67
?>


输出:
0 1 1 2 3 5 8 13 21 34


如何使用3个变量的2个变量打印?

具有2个变量的算法是:
1.打印a + b。
2.将a添加到b。
3.将b – a分配给a。

C++

// CPP Program to print Fibonacci sequence
// using 2 variables
#include 
using std::cout;
void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        cout << a << " ";
    if (n >= 1)
        cout << b << " ";
    for (int i = 2; i <= n; i++) {
        cout << a + b << " ";
        b = a + b;
        a = b - a;
    }
}
  
// Driver code
int main()
{
    fib(9);
    return 0;
}

Java

// Java Program to print 
// Fibonacci sequence
// using 2 variables
import java.io.*;
class GFG {
      
static void fib(int n)
{
    int a = 0, b = 1;
    if (n >= 0)
        System.out.print(a + " ");
    if (n >= 1)
        System.out.print(b + " ");
    for (int i = 2; i <= n; i++) 
    {
        System.out.print(a + b + " ");
        b = a + b;
        a = b - a;
    }
}
  
    // Driver code
    public static void main (String[] args) 
    {
        fib(9);
    }
}
  
// This code is contributed by anuj_67.

Python3

# Simple Python3 Program to
# print Fibonacci sequence
# using 2 variables
  
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):
        print(a + b, end=' ')
        b = a + b
        a = b - a
  
# Driver code
fib(9)
  
# This code is contributed by
# Prasad Kshirsagar

C#

// C# Program to print Fibonacci
// sequence using 2 variables
using System;
  
class GFG {
          
    static void fib(int n)
    {
        int a = 0, b = 1;
          
        if (n >= 0)
            Console.Write(a + " ");
        if (n >= 1)
            Console.Write(b + " ");
        for (int i = 2; i <= n; i++) 
        {
            Console.Write(a + b + " ");
            b = a + b;
            a = b - a;
        }
    }
  
    // Driver code
    public static void Main () 
    {
        fib(9);
    }
}
  
// This code is contributed by anuj_67.

的PHP

= 0)
        echo $a , " ";
    if ($n >= 1)
        echo $b , " ";
    for ($i = 2; $i <= $n; $i++)
    {
        echo $a + $b , " ";
        $b = $a + $b;
        $a = $b - $a;
    }
}
  
    // Driver code
    fib(9);
      
// This code is contributed by anuj_67
?>
输出 :
0 1 1 2 3 5 8 13 21 34