modify this code and add sum fibonacci number from below code: import java.util.Scanner;…
- User
modify this code and add sum fibonacci number from below code: import java.util.Scanner; public class he { public static void main(String[] args) { Scanner feb = new Scanner(System.in); System.out.print("Enter a number: "); int n = feb.nextInt(); int n1 = 1; int n2 = 1; System.out.print(n1 + " " + n2 + " ");
while (n2<=n) { int next = n1 + n2; System.out.print(n2 + " "); n1 = n2; n2 = next;} } } - Assistant
To modify the provided code to calculate and print the sum of Fibonacci numbers up to a given value ( n ), we need to add a variable to keep track of the sum of the Fibonacci numbers generated in the loop. Additionally, since the original code prints some additional Fibonacci numbers and lacks control to stop at exactly ( n ), it is necessary to make correct adjustments to meet the requirement.