i made code that calculate fibonaci number (n)
but how i can determine the largest fibonaci number

``````import java .util.Scanner;
public class Fibo {

public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);

System.out.println(  ":Enter number " );

int N = input.nextInt();
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}

}``````

I do not understand what you mean!!
Look at your code, clearly that your function (a recursion) do not show us which num is the largest Fibonacci number!
As i know, there doesnt exist fibo(-1), but your code says there exists!
:-??

i was thinking the largest number in my code is ( number n)
just i should print n
its true ?

and what did you mean with exist ?

Oh, firstly, im sorry if i did not write clearly coz i dont speak English!
In your code, the largest number depends on the memory and the maximum number of int type!
And about "exist", as i know, fibo sequence start with i = 0, so i think fibo(-1) = NULL

no its okay . also i didn't speak english ..
see output :
--------------------Configuration: <Default>--------------------
:Enter number
5
1: 1
2: 1
3: 2
4: 3
5: 5

so the largest number here is 5
its true what iam thinking about it

;))
That's Fibonaci?
If n = 4, it returns 3, => it's not true!

Oh, firstly, im sorry if i did not write clearly coz i dont speak English!
In your code, the largest number depends on the memory and the maximum number of int type!
And about "exist", as i know, fibo sequence start with i = 0, so i think fibo(-1) = NULL

There should be an else if statement that returns 0 if "i=0" to avoid fibo(-1) and replace "i<=1" to "i==1"

If you want to have the next Fibonacci number then just add 1 to N(the value you pass)

I guess largest number is what u entered in consol.. In your example largest number is 5.

``````Scanner input=new Scanner(System.in);
System.out.println(  ":Enter number " );
int N = input.nextInt();
System.out.println("Largest number is " + N);``````

I guess largest number is what u entered in consol.. In your example largest number is 5.

He want to know the largest Fibo number, as we know, this is fibo sequence:
0,1,1,2,3,5,8,13,21...
According to what u said, if we enter number 6, we get the largest fibo number is 6? But 6 doesn't appear in above sequence!
With his code, when you enter a number N, to find the largest Fibonacci number, it will occur one of 2 case below:
1) Largest Fibo = is Fibo(N) if enough memory and N not greater than the maximum value of Int type.
2) Error occurred if not enough memory (because he use recursion => use much memory to hold values in stack) and N is too large (greater than 2^n-1 if sizeof(int) = n)
---
Songokute

;))
That's Fibonaci?
If n = 4, it returns 3, => it's not true!

actually, for n = 4, 3 is the correct result.

what you can do is something like this:

``````int n = input();
if ( n < 1 )
System.out.println("ERROR");
else{
int number = 1;
int var1 = 1;
int var2 = 1;

while ( var2 <= n ){
var1 = var2;
var2 = calucalteNextFibon(var1, var2);
if ( var2 <= n )
number = var2;
}

System.out.print("number = " + number);
}``````

you already know how to find the next fibonnaci number, so ... just implement it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.