Java Program to Print Reverse Fibonacci Series Elements
Here's a Java program named 'TestFibonacci.java' that reads two non-negative numbers from the console (n and d) and outputs d elements of the Fibonacci series starting from the n-th element in reverse order:
import java.util.Scanner;
public class TestFibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();
System.out.print("Enter the value of d: ");
int d = scanner.nextInt();
int[] fibonacci = new int[n + d]; // Array to store the Fibonacci series
fibonacci[0] = 1;
fibonacci[1] = 1;
// Generating the Fibonacci series
for (int i = 2; i < n + d; i++) {
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
// Outputting d elements of the series in reverse order
for (int i = n - 1; i >= n - d; i--) {
System.out.print(fibonacci[i] + " ");
}
scanner.close();
}
}
Please note that this program assumes the input values n and d are non-negative integers not exceeding 100000.
原文地址: https://www.cveoy.top/t/topic/bfBL 著作权归作者所有。请勿转载和采集!