Following is a simple program to print first n Fibonacci numbers in java.
Examples :
Input : n = 3 Output : 0 1 1 Input : n = 7 Output : 0 1 1 2 3 5 8
IN JAVA:
// Java program to print // first n Fibonacci Numbers class Test { // Method to print // first n Fibonacci Numbers static void printFibonacciNumbers(int n) { int f1 = 0, f2 = 1, i; if (n < 1) return; System.out.print(f1 + " "); for (i = 1; i < n; i++) { System.out.print(f2 + " "); int next = f1 + f2; f1 = f2; f2 = next; } } // Driver Code public static void main(String[] args) { printFibonacciNumbers(7); } }
Output
0 1 1 2 3 5 8
Time Complexity: O(n)
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.