In this post we will see Java Code for Fibonacci series. we will solve fibonacci series first with an algorithm then will write actual code in java.
What is Fibonacci Series
A series where next number is the addition of previous two numbers is called as fibonacci series.
Example:
1 1 2 3 5 8 13 21 24………….
Here we can observe first letter is 1 and second letter is 1, if we add we get 2 which is the third number
ie 1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
8+13=21
13+21=24
It continues …………….
Algorithm For Fibonacci Series
- Initialize first two numbers as 1 that is a=1,b=1
- Next print first two numbers
- Then add first 2 assign it to variable c. ie c=a+b
- Print c
- Now re-assign a=b and c=b.
- Repeat from 4 until end
- Congrats you just solved one problem, we have similar type of questions Click Here to take a look.
Java Code For Fibonacci Series
<strong> // Code by DATTATREY PATIL public class problem30 { public static void main(String[] args) { System.out.println("Fibonachi series "); int a,b,c; a=1; b=1; // Here we are printing 20 terms in fibonacci series int n=20; System.out.println(a); System.out.println(b); for(int i=0;i<n;i++){ c=a+b; System.out.println(c); a=b; b=c; } } } }</strong>
Visit our channel for more explanation on code
Pingback: Java code for finding factors of a Number - Is It Actually
Pingback: Java code to find Factorial of Number - Is It Actually
Pingback: 3 Ways to Find Prime Number - Is It Actually