Sure! I can help you create two Java programs that display the Fibonacci series based on a user-inputted positive integer (from 0 to 15). One program will use a for loop, and the other will use a while loop. Below are the source codes along with explanations and sample outputs.
1. Fibonacci Series Using a for Loop
import java.util.Scanner;
public class FibonacciForLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.println("This program displays the sequence based on the number entered by user.");
while (true) {
System.out.print("Input how many numbers you want in the sequence: ");
number = scanner.nextInt();
if (number >= 0 && number <= 15) {
System.out.print("Fibonacci Series of " + number + " numbers: ");
int n1 = 0, n2 = 1, n3;
for (int i = 0; i < number; i++) {
if (i == 0) {
System.out.print(n1 + " ");
} else if (i == 1) {
System.out.print(n2 + " ");
} else {
n3 = n1 + n2;
System.out.print(n3 + " ");
n1 = n2;
n2 = n3;
}
}
System.out.println("\nProgram Terminated...");
break;
} else {
System.out.println("INVALID! Input out of range. Please input again...");
}
}
scanner.close();
}
}
Explanation:
- We use a
Scanner to get user input.
- A
while (true) loop ensures we keep asking for input until a valid number is provided.
- The input is validated to be between 0 and 15 inclusive.
- If valid, we use a
for loop to compute and display the Fibonacci series.
- The program handles the first two numbers separately (
n1 and n2) and then calculates subsequent numbers.
- If the input is invalid, the user is prompted to input again.
- The program terminates after displaying the Fibonacci series.
Sample Outputs:
Sample Output 1 (Valid Input - 4):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 4
Fibonacci Series of 4 numbers: 0 1 1 2
Program Terminated...
Sample Output 2 (Valid Input - 7):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 7
Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Program Terminated...
Sample Output 3 (Invalid Input - 20, then Valid Input - 7):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 20
INVALID! Input out of range. Please input again...
Input how many numbers you want in the sequence: 7
Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Program Terminated...
2. Fibonacci Series Using a while Loop
import java.util.Scanner;
public class FibonacciWhileLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.println("This program displays the sequence based on the number entered by user.");
while (true) {
System.out.print("Input how many numbers you want in the sequence: ");
number = scanner.nextInt();
if (number >= 0 && number <= 15) {
System.out.print("Fibonacci Series of " + number + " numbers: ");
int n1 = 0, n2 = 1, n3, i = 0;
while (i < number) {
if (i == 0) {
System.out.print(n1 + " ");
} else if (i == 1) {
System.out.print(n2 + " ");
} else {
n3 = n1 + n2;
System.out.print(n3 + " ");
n1 = n2;
n2 = n3;
}
i++;
}
System.out.println("\nProgram Terminated...");
break;
} else {
System.out.println("INVALID! Input out of range. Please input again...");
}
}
scanner.close();
}
}
Explanation:
- Similar to the
for loop version, but we use a while loop to generate the Fibonacci series.
- The variable
i is initialized before the loop and incremented inside the loop.
- The rest of the logic remains the same, handling the first two numbers and calculating the rest.
- Input validation and prompting are handled in the same way.
Sample Outputs:
Sample Output 1 (Valid Input - 4):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 4
Fibonacci Series of 4 numbers: 0 1 1 2
Program Terminated...
Sample Output 2 (Valid Input - 7):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 7
Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Program Terminated...
Sample Output 3 (Invalid Input - 20, then Valid Input - 7):
This program displays the sequence based on the number entered by user.
Input how many numbers you want in the sequence: 20
INVALID! Input out of range. Please input again...
Input how many numbers you want in the sequence: 7
Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Program Terminated...
Instructions for Submission:
Feel free to ask if you have any questions or need further assistance with the code!