Fibonacci Sequence Using Methods in Java

import java.util.Scanner;

/* AniBlogger12
 * Methods 
 */
public class methods7 {
	// method for returning fibonacci sequence
	public static int fibonacci(int num) {
		// if the number is 0, return 0
		if (num == 0) {
			return 0;
			// or else if the number is 1, return 1
		} else if (num == 1) {
			return 1;
			// else return the sum of the 2 last numbers to get the next term in the
			// sequence
		} else {
			return fibonacci(num - 1) + fibonacci(num - 2);
		}
	}

	// main
	public static void main(String[] args) {
		// get keyboard
		Scanner kb = new Scanner(System.in);
		// prompt user
		System.out.println("Please enter the number of terms you want to display the fibonacci series:");
		// user input
		int num = kb.nextInt();
		// result variable
		int result = 0;
		// for loop
		for (int i = 0; i < num; i++) {
			// result is equal to the fibonacci of (i)
			result = fibonacci(i);
			// print out result
			System.out.println(result);
		}

	}
}
Design a site like this with WordPress.com
Get started