Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Fibonacci

farenda 2015-06-09 0

Problem:

How to find Fibonacci numbers in Java?

Solution:

The following program shows two methods that find Fibonacci numbers. printAllFibo(n) just prints first n Fibonacci numbers and nthFibo(n) returns n-th number from the series:

package com.farenda.solved;

public class JavaSolved {

    // Print first n numbers in Fibonacci series:
    public static void printAllFibo(int n) {
        int a = 0, b = 1;
        while (--n >= 0) {
            System.out.println(b);
            int tmp = b;
            b += a;
            a = tmp;
        }
    }

    // Return n-th Fibonacci number:
    public static int nthFibo(int n) {
        int a = 0, b = 1;
        while (--n >= 0) {
            int tmp = b;
            b += a;
            a = tmp;
        }
        return a;
    }

    public static void main(String[] args) {
        if (args.length == 1) {
            int n = Integer.parseInt(args[0]);
            System.out.printf("%d-th Fibonacci number: %d%n", n, nthFibo(n));
            System.out.printf("First %d Fibonacci numbers:%n", n);
            printAllFibo(n);
        } else {
            System.out.printf("Usage: %s number%n", JavaSolved.class.getSimpleName());
        }
    }
}

Note that this calculates Fibonacci numbers in an iterative way, because JVM doesn’t have TCO (Tail Call Optimization) as is found in many functional languages. Implementing it recursively would result in StackOverflowError for larger numbers!

Result:

As always, lets compile and run it to see the results:

$> javac src/com/farenda/solved/*.java -d out
$> java -cp out com.farenda.solved.JavaSolved 10
10-th Fibonacci number: 55
First 10 Fibonacci numbers:
1
1
2
3
5
8
13
21
34
55
Share with the World!
Categories Java Tags algorithms, java-basics
Previous: Java foreach
Next: EmbedMongo error libssl libcrypto

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok