Fibonacci Series
0 Answers
Fibonacci Series
| Flag | PURGE
The code posted below, It's not getting compiled because of Array initialization inside class but when that's commented, code works fine. why is it so ?
class getFib
{
int[] Fib = new int[100];
Fib[0] = 0;
Fib[1] = 1;
Fib[2] = 1;
int getFib(int n)
{
int i;
int[] Fib = new int[100];
Fib[0] = 0;
Fib[1] = 1;
Fib[2] = 1;
for(i = 3; i <= n; i++)
{
Fib[i] = Fib[i-1] + Fib[i-2];
System.out.println("Fib"+"["+i+"] = "+Fib[i]);
}
return Fib[i];
}
}
class Fibonac
{
public static void main(String args[])
{
getFib f = new getFib();
System.out.println("Fibonacci Series for 2000 = "+ f.getFib(10));
}
}
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
Email me when people comment.
Email me when people comment.
Loading...
An error occurred in subscribing you.
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.