Fibonacci Series Example in Java

public class FibonacciSeriesExample {
	
	public static void printFibonacciSeries(int num){
		System.out.println("\n\nFibonacci Series of "+num +" is:");
		int prev = 0;
		int current = 1;
	
		int temp = 0;
		
		System.out.print(prev+",");
		
		while (current <= num) {
			System.out.print(current+",");
			
			temp = current;
			current += prev;
			prev = temp;
		}
	}
	public static void main(String[] args) {
		System.out.print("FibonacciSeriesExample");
		printFibonacciSeries(100);
		printFibonacciSeries(1);
		printFibonacciSeries(34);
	}
}
Output
FibonacciSeriesExample Fibonacci Series of 100 is: 0,1,1,2,3,5,8,13,21,34,55,89 Fibonacci Series of 1 is: 0,1,1 Fibonacci Series of 34 is: 0,1,1,2,3,5,8,13,21,34










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: