public class FibonacciGenerator {
public static void main(String[] args) {
int numberMembers = 10; // Set the limit for the Fibonacci series
generateFibonacci(numberMembers); // Generate and print the Fibonacci series
}
// Function to generate Fibonacci series up to a given limit
public static void generateFibonacci(int limit) {
// Check if the limit is less than or equal to 0
if (limit <= 1) {
System.out.println("Please provide a valid number of members greater than 1.");
return;
}
// Initialize the first two numbers in the series
int num1 = 0, num2 = 1;
// Print the first two numbers
System.out.print("Fibonacci series up to " + limit + " members: " + num1 + ", " + num2);
// Generate the Fibonacci series
for (int i = 1; i < limit-1; i++) {
int nextNum = num1 + num2;
// Print the next number
System.out.print(", " + nextNum);
// Update num1 and num2 for the next iteration
num1 = num2;
num2 = nextNum;
}
}
}
Want 1 to 1 personalized Java training? Email me at isingh30 AT gmail please. View my following video
:
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.