Java 程序:打印斐波那契数列
原文:https://www.studytonight.com/java-programs/java-program-to-print-fibonacci-series
在本教程中,我们将学习如何打印斐波那契数列。斐波那契数列是下一个数是前两个数之和的数列。但是在继续之前,如果你不熟悉 java 中循环的概念,那么一定要查看关于 Java 中循环的文章。
输入:输入数字:10
输出:斐波那契数列为:
0 1 1 2 3 5 8 13 21 34
上述问题可以通过以下方式解决:
方法 1:使用 for 循环
方法 2:使用 while 循环
方法 3:打印给定数量的系列
方法 4:使用递归函数。
让我们分别看看这些方法。
程序 1:打印斐波那契数列
在这个程序中,我们将看到如何使用 for 循环在 Java 中打印斐波那契数列。在这里,首先,我们将要求用户输入项数,然后我们将找到斐波那契数列。
算法:
- 开始
- 为术语总数声明一个变量。
- 要求用户初始化术语的数量。
- 打印系列的第一个和第二个数字。
- 使用 for 循环打印多达该数量项的斐波那契数列。
- 在每次迭代中更新系列术语。
- 打印斐波那契数列。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to print Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.println("First " + n + " terms of fibonnaci series: ");
//Print the fibonacci series
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
输入项数:10 斐波那契数列的前 10 项: 0 1 2 3 5 8 13 21 34
程序 2:打印斐波那契数列
在这个程序中,我们将看到如何使用 while 循环在 Java 中打印斐波那契数列。在这里,首先,我们将要求用户输入项数,然后我们将找到斐波那契数列。
算法:
- 开始
- 为术语总数声明一个变量。
- 要求用户初始化术语的数量。
- 打印系列的第一个和第二个数字。
- 使用 while 循环打印斐波那契数列,直到达到该项数。
- 在每次迭代中更新系列术语。
- 打印斐波那契数列。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to print the Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.println("First " + n + " terms of fibonnaci series: ");
//Print the fibonacci series
int i = 1;
while (i <= n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
i++;
}
}
}
输入项数:10 斐波那契数列的前 10 项: 0 1 2 3 5 8 13 21 34
程序 3:打印斐波那契数列
在这个程序中,我们将看到如何在 Java 中打印给定数字的斐波那契数列。在这里,首先,我们将要求用户输入项数,然后我们将找到斐波那契数列,直到那个特定的数字。
算法:
- 开始
- 声明一个变量。
- 要求用户初始化数字。
- 打印系列的第一个和第二个数字。
- 使用 while 循环打印斐波那契数列到那个数字。
- 在每次迭代中更新系列术语。
- 打印斐波那契数列。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to print the Fibonacci series
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Enter the number: ");
int n=sc.nextInt(); //Declare and Initialize the number
System.out.println("Fibonnaci series upto "+n+": ");
//Print the fibonacci series
while (t1 <= n)
{
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
输入数字:30 Fibonnaci 系列到 30: 0 1 2 3 5 8 13 21
程序 4:打印斐波那契数列
在这个程序中,我们将看到如何使用递归在 Java 中打印斐波那契数列。在这里,首先,我们将要求用户输入项数,然后我们将找到斐波那契数列。
算法:
- 开始
- 为术语总数声明一个变量。
- 要求用户初始化术语的数量。
- 打印系列的第一个和第二个数字。
- 调用一个递归函数来打印斐波那契数列的项数。
- 递归更新系列术语。
- 打印斐波那契数列。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to print Fibonacci series
import java.util.*;
public class Main
{
static int n1=0,n2=1,n3=0;
//Prints Fibonacci Series using Recursion
static void printFibonacci(int n)
{
if(n>0)
{
n3 = n1 + n2;
System.out.print(" "+n3);
n1 = n2;
n2 = n3;
printFibonacci(n-1);
}
}
public static void main(String args[])
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.print("Fibonacci Series up to "+n+" terms: ");
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(n-2);
}
}
输入项数:12 斐波那契数列最多 12 项: 0 1 2 3 5 8 13 21 34 55 89