no1 = 0
and no2 = 1
.
no1
and no2
.
no1 + no2
.
no1 = no2
and no2 = sum
in each iteration.
public class FibonacciSeries
{
public static void main(String[] args)
{
int n = 10; // Number of terms to print
int no1 = 0, no2 = 1;
System.out.print("Fibonacci Series: " + no1 + ", " + no2);
for (int i = 2; i < n; i++)
{
int sum = no1 + no2;
System.out.print(", " + sum);
no1 = no2;
no2 = sum;
}
}
}
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
no1 = 0
and no2 = 1
are initialized to store the first two numbers of the Fibonacci series.
n
(the number of terms). In each iteration:
sum = no1 + no2
.
no1
to no2
and no2
to sum
for the next iteration.
n
terms are printed.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.