Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fibonacci Series In Java

user-nlz8hMar 26 2023

I built a programme in Java to display the Fibonacci series, but now I want the numbers in the sequence to be presented in tens, that is, the first 10 fibonacci numbers on one line, followed by the following ten on the next line, and so on.

Here is the schedule:

import java.util.Scanner;
import java.math.BigInteger;

class Fibonacci {

public static void main(String args[]) {


    System.out.print("Enter number upto which Fibonacci series to print: ");
    int number = new Scanner(System.in).nextInt();

    System.out.println("\n\nFibonacci series upto " + number + " numbers : ");

    for (int i = 1; i <= number; i++) {

        System.out.println(fibonacciLoop(i) + " ");
    }
}

public static BigIntger fibonacciLoop(int number) {
    if (number == 1 || number == 2) {
        return BigInteger.valueOf(1);
    }
    for (int x = 1; x <= number; x++){
        return BigInteger.valueOf(x);
    }
    BigInteger fibonacci = BigInteger.valueOf(1);
    BigInteger fibo1 = BigInteger.valueOf(1);
    BigInteger fibo2 = BigInteger.valueOf(1);
    for (int i = 3; i <= number; i++) {
        fibonacci = fibo1.add(fibo2);
        fibo1 = fibo2;
        fibo2 = fibonacci;
    }
    return fibonacci;
}
}
This post has been answered by Solomon Yakobson on Mar 26 2023
Jump to Answer
Comments
Post Details
Added on Mar 26 2023
4 comments
1,818 views