Last active
December 11, 2020 08:26
-
-
Save vividvilla/4641152 to your computer and use it in GitHub Desktop.
Fibonacci Series in Java using for Loop - http://vivek.techiestuffs.com/2012/fibonacci-series-in-java-using-for-loop/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Program for Printing first N terms of Fibonacci series using for loop (without taking input from user) | |
* Code by Vivek R (vividvilla) - Contact me at - http://vivek.techiestuffs.com | |
*/ | |
public class FibonacciFor { | |
public static void main(String[] args) | |
{ | |
int n=10,first=0,second=1,next,i; | |
System.out.println("Printing first "+n+" numbers in Fibonacci Series \n"); | |
for(i=0;i<n;i++) | |
{ | |
if(i<=1) | |
next=i; | |
else | |
{ | |
next = first + second; | |
first = second; | |
second = next; | |
} | |
System.out.print(next + "\t"); | |
} | |
} | |
} |
I am excited to begin the programming journey
int f_n, f_n_1, f_n_2, n;
int i =0;
Scanner input = new Scanner (System.in);
System.out.print("mengambil fibonacci ke - ");
n = input.nextInt();
f_n_2 = 1;
f_n_1 = 0;
f_n = 0;
for(i = 0; i <= n; i++){
System.out.println("nilai ke - " + (i+1) + " adalah "+ f_n);
f_n = f_n_1 + f_n_2;
f_n_2 = f_n_1;
f_n_1 = f_n;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fibonacci(N) = Nth term in fibonacci series
fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
whereas, fibonacci(0) = 0 and fibonacci(1) = 1