Computer Applications
Write a program in Java to find the Fibonacci series within a range entered by the user.
Sample Input:
Enter the minimum value: 10
Enter the maximum value: 20
Sample Output:
13
Answer
import java.util.Scanner;
public class KboatFibonacciRange
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the minimum value: ");
int m = in.nextInt();
System.out.print("Enter the maximum value: ");
int n = in.nextInt();
int a = 0, b = 1;
while (a <= n) {
if (a >= m)
System.out.println(a);
int c = a + b;
a = b;
b = c;
}
}
}Output
Related Questions
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );