Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active December 11, 2020 08:26
Show Gist options
  • Save vividvilla/4641152 to your computer and use it in GitHub Desktop.
Save vividvilla/4641152 to your computer and use it in GitHub Desktop.
/* 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");
}
}
}
@arunkumardancer
Copy link

fibonacci(N) = Nth term in fibonacci series
fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
whereas, fibonacci(0) = 0 and fibonacci(1) = 1

@MakNdede
Copy link

MakNdede commented Jun 7, 2018

I am excited to begin the programming journey

@Viandra-cyber
Copy link

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