The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 . . .
Given a specific number in this series Fn, your program needs to find the sum of all the numbers in the Fibonacci series that are smaller or equal to that number.
Input Format Your program will be provided a number in the Fibonacci series on STDIN.
Constraints
0<<Fn<100000
Sample Input
8
Sample Output
The output for above input (8) should be 0 + 1 + 1 + 2 + 3 + 5 + 8
20
To solve above problem I write a code like
import java.util.*;
import java.lang.*;
import java.io.*;
class MyTest{
public static Integer solveProblem(int n) {
if (n>=0 || n<100000) {
int fibo[] = new int[n+1];
fibo[0] = 0; fibo[1] = 1;
int sum = fibo[0] + fibo[1];
for (int i=2; i<=n; i++) {
fibo[i] = fibo[i-1]+fibo[i-2];
if(n >= fibo[i]) {
sum += fibo[i];
} else {
break;
}
}
return sum;
} else {
return 0;
}
}
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
int sum = MyTest.solveProblem(in.nextInt());
System.out.println("This is the output:"+sum);
}
}
I run the program it works fine with the sample test case as mentioned but when I submit this program on online test quiz then it run with their test case and it will fail.
Is something wrong in my program or I didn't understand the question properly. Please help me to find out exact answer of this problem.