Write Java Program to Print Fibonacci Series up-to N Numbers

In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:

By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation.

with seed values

Fibonacci Series Example - crunchify.co

Here is a simplest Java Program to generate Fibonacci Series.

Method-1 and Method-2 in a single program

package crunchify.com.tutorials;

import java.util.Scanner;

/**
 * @author Crunchify.com
 * 
 */
public class CrunchifyFibonacci {

        @SuppressWarnings("resource")
        public static void main(String args[]) {

                // input to print Fibonacci series upto how many numbers
                log("Enter number upto which Fibonacci series to print: ");
                int number = new Scanner(System.in).nextInt();

                log("\nUsing Method-1: Using Recursion. Provided Number: " + number);
                // printing Fibonacci series upto number
                for (int i = 1; i <= number; i++) {
                        log(fibonacciRecusion(i) + " ");
                }

                log("\nMethod-2: Fibonacci number at location " + number + " is ==> " + (fibonacciLoop(number) + ""));

        }

        // Method-1: Java program for Fibonacci number using recursion.
        public static int fibonacciRecusion(int number) {
                if (number == 1 || number == 2) {
                        return 1;
                }

                return fibonacciRecusion(number - 1) + fibonacciRecusion(number - 2); // tail recursion
        }

        // Method-2: Java program for Fibonacci number using Loop.
        public static int fibonacciLoop(int number) {
                if (number == 1 || number == 2) {
                        return 1;
                }
                int fibo1 = 1, fibo2 = 1, fibonacci = 1;
                for (int i = 3; i <= number; i++) {
                        fibonacci = fibo1 + fibo2; // Fibonacci number is sum of previous two Fibonacci number
                        fibo1 = fibo2;
                        fibo2 = fibonacci;

                }
                return fibonacci; // Fibonacci number
        }

        private static void log(String number) {
                System.out.println(number);

        }

}

Result of method-3 and method-4:

Enter number upto which Fibonacci series to print: 
10

Using Method-1: Using Recursion. Provided Number: 10
1 
1 
2 
3 
5 
8 
13 
21 
34 
55 

Method-2: Fibonacci number at location 10 is ==> 55

Method-3

Thank you S.Sur for posting code as a comment.

package crunchify.com.tutorials;

import java.util.Scanner;

public class CrunchifyFibonacciOption3 {
        public static void main(String args[]) {
                
                System.out.println("Enter the term to be printed");
                Scanner ob = new Scanner(System.in);
                int ch = ob.nextInt();
                System.out.println("The" + ch + " terms of fibanocci numbers are-");
                int a, b, s, n;
                a = b = 1;
                for (n = 1; n <= ch; n++) {
                        System.out.println(a);
                        s = a + b;
                        a = b;
                        b = s;
                }
        }
}

Result of method-3

Enter the term to be printed
5
The5 terms of fibanocci numbers are-
1
1
2
3
5

Method-4

package crunchify.com.tutorials;

import java.util.Scanner;

public class CrunchifyFibonacciOption4 {
        public static void main(String args[]) {

                System.out.println("Enter the term to be printed");
                Scanner ob = new Scanner(System.in);
                int a = 1;
                int b = 0;
                int ch = ob.nextInt();
                ;
                for (int i = 0; i < ch; i++) {
                        System.out.print(b);
                        b = a + b;
                        a = b - a;
                        if (i <= ch - 2) {
                                System.out.print(", ");
                        }
                        if (i == ch - 1) {
                                System.out.println(".\nThese are the first " + ch + " Fibonacci Numbers!");
                        }
                }
        }
}

Result of method-4:

Enter the term to be printed
5
0, 1, 1, 2, 3.
These are the first 5 Fibonacci Numbers!

The post Write Java Program to Print Fibonacci Series up-to N Number [4 different ways] appeared first on Crunchify.