Write a Java program to print Fibonacci series of n terms where n is input by user using loop
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.Scanner; public class JavaLoopExcercise { public static void main(String[] args) { Scanner console = new Scanner(System.in); int number; int firstTerm = 0, secondTerm = 1, thirdTerm; System.out.print("Enter number of terms of series : "); number = console.nextInt(); System.out.print(firstTerm + " " + secondTerm + " "); for(int i = 3; i <= number; i++) { thirdTerm = firstTerm + secondTerm; System.out.print(thirdTerm + " "); firstTerm = secondTerm; secondTerm = thirdTerm; } } }
Result:
Enter number of terms of series : 10
0 1 1 2 3 5 8 13 21 34
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
Result:
Enter number of terms of series : 10
0 1 1 2 3 5 8 13 21 34
need an explanation for this answer? contact us directly to get an explanation for this answer