Algorithm
- Start
- Initialize variables
n
(number of terms) andfirst
andsecond
as 0 and 1 respectively. - Print the first and second terms (
first
andsecond
). - Use a loop to generate the Fibonacci series: a. Loop from 3 to
n
. b. Calculate the next termnext
as the sum offirst
andsecond
. c. Printnext
. d. Updatefirst
to the value ofsecond
. e. Updatesecond
to the value ofnext
. - End
Code Examples
#1 Code Example- Display Fibonacci Series Using for Loop
Code -
Java Programming
class Main {
public static void main(String[] args) {
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i < = n; ++i) {
System.out.print(firstTerm + ", ");
// compute the next term
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Copy The Code &
Try With Live Editor
Output
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Demonstration
Java Programing Example to Display Fibonacci Series-DevsEnv