Write a Java program that will print the following series until it reaches 100.
1 1 2 3 5 8 13 …………………………
class Fibonacci {
public static void main(String[] args) {
int n = 100, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series Upto " + n + ": ");
while (firstTerm <= n) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Comments
Leave a comment