Fixing a Java Program to Print the Last Two Numbers in a Series

How can I fix this code?

I got 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 but I need only 1134903170 1836311903.

Final answer:

To fix the code and print only the last two numbers in the series, you need to modify the loop condition to 'i<2'. Here is the modified code:

Explanation: To fix the code, you need to modify the loop condition and add a condition to stop the loop after the second iteration. Currently, the loop condition is 'firstNumber+secondNumber>=0', which means the loop will continue as long as the sum of the firstNumber and secondNumber is greater than or equal to zero.

To print only the last two numbers in the series, you can change the loop condition to 'i<2'. This will ensure that the loop stops after the second iteration.

Here is the modified code:

public class Main { public static void main(String[] args) { int firstNumber = 1; // start at number 1 and 2 int secondNumber = 2; int nextNumber; int i; for( i=1; i<2; i++) { nextNumber = firstNumber+secondNumber; System.out.print(" " + nextNumber); firstNumber = secondNumber; secondNumber = nextNumber; firstNumber } } }

← What is the significance of abs in vehicles C3 ai key features and differences from silicon valley unicorns →