import java.util.Scanner; public class Fibonacci { public static void main(Strin
ID: 3902994 • Letter: I
Question
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int x;
@SuppressWarnings("resource")
Scanner limitEntery = new Scanner(System.in);
System.out.print("Enter x under 100: ");
x = limitEntery.nextInt();
if(x>=92)
{
System.out.println("Sorry the limit is 91!!!");
}
else {
long[] series = new long[x];
series[0] = 0;
series[1] = 1;
for(int i=2; i < x; i++){
series[i] = series[i-1] + series[i-2];
}
System.out.println("Fibonacci Series are up to " + x);
for(int i=0; i< x; i++){
System.out.print(series[i] + " ");
}
for(int i=x-1; i >= 0; i--){
System.out.print(series[i] + " ");
}
}
}
}
I have a code, but I also want to get the answer backward.
So there will be 2 answers, the first one is the forward and the backward.
Explanation / Answer
Fibonacci.java
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int x;
@SuppressWarnings("resource")
Scanner limitEntery = new Scanner(System.in);
System.out.print("Enter x under 100: ");
x = limitEntery.nextInt();
if(x>=92)
{
System.out.println("Sorry the limit is 91!!!");
}
else {
long[] series = new long[x];
series[0] = 0;
series[1] = 1;
for(int i=2; i < x; i++){
series[i] = series[i-1] + series[i-2];
}
System.out.println("Fibonacci Series are up to " + x);
for(int i=0; i< x; i++){
System.out.print(series[i] + " ");
}
System.out.println();
for(int i=x-1; i >= 0; i--){
System.out.print(series[i] + " ");
}
}
}
}
Output:
Enter x under 100: 10
Fibonacci Series are up to 10
0 1 1 2 3 5 8 13 21 34
34 21 13 8 5 3 2 1 1 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.