This is a Java Program to Generate Fibonacci Numbers. The number is said to be in a Fibonacci series if each subsequent number is the sum of the previous two numbers.
Enter the number of terms you want as an input. Now we use for loop to generate the desired series.
Here is the source code of the Java Program to Generate Fibonacci Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}
Output:
$ javac Fibonacci.java $ java Fibonacci Enter value of n:5 Fibonacci Series:0 1 1 2 3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice BCA MCQs
- Apply for Java Internship
- Practice Programming MCQs
- Apply for Information Technology Internship
- Practice Information Technology MCQs