Computer Applications
Write a program to print Fibonacci series : 0, 1, 1, 2, 3, 5, 8….
Java
Java Iterative Stmts
17 Likes
Answer
public class KboatFibonacci
{
public static void main(String args[]) {
int a = 0;
int b = 1;
System.out.print(a + " " + b);
/*
* i is starting from 3 below
* instead of 1 because we have
* already printed 2 terms of
* the series. The for loop will
* print the series from third
* term onwards.
*/
for (int i = 3; i <= 20; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
}
}
Variable Description Table
Program Explanation
Output
Answered By
8 Likes
Related Questions
Write a program to print factorial of a given number.
Write a program to print Floyd's triangle as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Write a program to input a number in the range 10 to 100 and check if it is a prime number.
Write a program to print following series of numbers: 2, 5, 8, 11, 14….