C++ Saved Program: placid-city

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


mport java.util.Scanner;

 public class FibonacciSeries {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        int n = s.nextInt();
        fibonacci(n);
    }

    public static void fibonacci(int n) {
        if (n == 0) {
            System.out.println("0");
        } else if (n == 1) {
            System.out.println("0 1");
        } else {
            System.out.print("0 1 ");
            int a = 0;
            int b = 1;
            for (int i = 1; i < n; i++) {
                int nextNumber = a + b;
                System.out.print(nextNumber + " ");
                a = b;
                b = nextNumber;
            }
        }
    }
 }
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs