Fibonacci, Extends and Data Modeling
Fibonacci, Extends and Data Modeling
import java.util.Scanner;
public class FLoop {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series for the first " + n + " terms:");
for (int i = 1; i <= n; i++) {
System.out.print(firstTerm + " ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
FLoop.main(null);
import java.util.Scanner;
public class WLoop {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int firstTerm = 0, secondTerm = 1, i = 1;
System.out.println("Fibonacci Series for the first " + n + " terms:");
while (i <= n) {
System.out.print(firstTerm + " ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
i++;
}
}
}
WLoop.main(null);
public class Recursion {
public static void main(String[] args) {
int lastTerm = 0, nextTerm = 1;
Scanner sc = new Scanner(System.in);
int maxTermLength = sc.nextInt();
System.out.println("How many terms do you want in the Fibonacci Sequence? " + maxTermLength);
System.out.println("Fibonacci Sequence with " + maxTermLength + " terms.");
for (int i = 1; i <= maxTermLength; ++i) {
System.out.print(lastTerm+" ");
int sum = lastTerm + nextTerm;
lastTerm = nextTerm;
nextTerm = sum;
}
}
}
Recursion.main(null);