Java Fibonacci Series

  1. Generate Fibonacci Series upto N times in Java
  2. Set Max Value to Fibonacci Series in Java
  3. Generate Fibonacci Series within a interval
  4. Set Starting value and N times to Fibonacci Series

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Generate Fibonacci Series upto N times in Java


Fibonacci_Ex1.java

import java.util.Scanner;
 
public class Fibonacci_Ex1 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter 'N' value  : ");
		
		int n = Integer.parseInt(scan.nextLine());
		
		int a = -1;
		int b = 1;
		int c = 0;
		
		System.out.println("The Fibonacci Series are : ");
 
		for(int i=1; i<=n; i++) {
			
			c = a + b;
			int temp = a;
			a = b;
			b = c;
			
			System.out.println(c);
		}
	}
}

Sample Output

Enter 'N' value  : 
10
The Fibonacci Series are : 
0
1
1
2
3
5
8
13
21
34




B. Set Max Value to Fibonacci Series in Java


Fibonacci_Ex2.java

import java.util.Scanner;
 
public class Fibonacci_Ex2 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter Max value : ");
		
		int max = Integer.parseInt(scan.nextLine());
		
		int a = -1;
		int b = 1;
		int c = 0;
		
		c = a + b;
		
		System.out.println("The Fibonacci Series are : ");
		
		while(c <= max) {
						
			int temp = a;
			a = b;
			b = c;
			
			System.out.println(c);
			
			c = a + b;
		}
	}
}

Sample Output

Enter Max value : 
20
The Fibonacci Series are : 
0
1
1
2
3
5
8
13




C. Generate Fibonacci Series within a interval


Fibonacci_Ex3.java

import java.util.Scanner;
 
public class Fibonacci_Ex3 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter Starting value : ");
		int start = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter Ending value : ");
		int end = Integer.parseInt(scan.nextLine());
		
		int a = start;
		int b = 1;
		int c = 0;
		
		c = start;
		
		System.out.println("The Fibonacci Series are : ");
		
		while(c <= end) {
						
			int temp = a;
			a = b;
			b = c;
			
			System.out.println(c);
			
			c = a + b;
		}
	}
}

Sample Output

Enter Starting value : 
11
Enter Ending value : 
100
The Fibonacci Series are : 
11
12
23
35
58
93




D. Set Starting value and N times to Fibonacci Series


Fibonacci_Ex4.java

import java.util.Scanner;
 
public class Fibonacci_Ex4 {
	
	public static void main(String args[]) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter Starting value : ");
		int start = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter Number of value : ");
		int n = Integer.parseInt(scan.nextLine());
		
		int a = start;
		int b = 1;
		int c = 0;
		
		c = start;
		
		System.out.println("The Fibonacci Series are : ");
		
		for(int i=1; i<=n; i++) {
						
			int temp = a;
			a = b;
			b = c;
			
			System.out.println(c);
			
			c = a + b;
		}
	}
}

Sample Output

Enter Starting value : 
20
Enter Number of value : 
10
The Fibonacci Series are : 
20
21
41
62
103
165
268
433
701
1134






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com