Fibonacci Java
Certainly, here’s a simple Java program that generates Fibonacci numbers using an iterative approach:
public class Fibonacci {
public static void main(String[] args) {
int n = 10; // Change 'n' to the desired number of Fibonacci numbers to generate
generateFibonacci(n);
}
public static void generateFibonacci(int n) {
long[] fibonacciNumbers = new long[n];
fibonacciNumbers[0] = 0; // The first Fibonacci number is 0
fibonacciNumbers[1] = 1; // The second Fibonacci number is 1
for (int i = 2; i < n; i++) {
fibonacciNumbers[i] = fibonacciNumbers[i – 1] + fibonacciNumbers[i – 2];
}
System.out.println("Fibonacci Numbers:");
for (int i = 0; i < n; i++) {
System.out.print(fibonacciNumbers[i] + " ");
}
}
}
In this program, you can change the value of the n
variable to specify how many Fibonacci numbers you want to generate. When you run the program, it will print the specified number of Fibonacci numbers in the console.
For example, if you set n
to 10, the program will generate and print the first 10 Fibonacci numbers:
Fibonacci Numbers:
0 1 1 2 3 5 8 13 21 34
You can adjust the value of n
to generate a different number of Fibonacci numbers as needed.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks