can not add the 2 numbers

class fibo

{

int a;

int b;

int c;

int x;

boolean flag;

fibo(int x)

{

int a=2;

int b=3;

this.x=x;

System.out.println("the value of a : " + a);

System.out.println("the value of b : " + b);

flag=true;

}

void fibonoci()

{

for(int n=1; n<=x; n++)

{

if(flag==true)

{

c=a+b;

System.out.println(c);

a=c;

c=0;

flag=false;

}

else

{

c=a+b;

System.out.println(c);

b=c;

c=0;

flag=true;

}

}

}

}

class fibonocis

{

public static void main(String args[])

{

fibo fib=new fibo(10);

fib.fibonoci();

}

}

this is the program i have written to get the answer in fibonoci series but i am not abel to get the answer please help out

[898 byte] By [d_e_e_pa] at [2007-11-26 16:41:06]
# 1
Read this: http://forum.java.sun.com/help.jspa?sec=formattingAnd then tell us what's wrong. Real output? Expected output?
CeciNEstPasUnProgrammeura at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 2
i just wanted the answer as 2 3 5 8 13 22 35 57 92149but i get just 000000000000
d_e_e_pa at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 3
Post it using code tags [ http://forum.java.sun.com/help.jspa?sec=formatting ]Step through it with pen and paper.
mlka at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 4
use system out at each step n trace were its going wrong dude
LexusIs200a at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 5

Have a look at your Constructor. The basic mistake you made is the reason why there is a convention to name member variables with m_...

You redefined a and b in your Constructor and so the memver variable a and b of your class never got initialized (they are still 0).

But there is another mistake as well. Even if you correct his mistake the resulting series of integers seems not be correct.

g_magossa at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 6

> In the privacy of their own homes/business it isn't.

> But in a public forum, it is very much open to me to

> point out that the person is misrepresenting his own

> personal choice as a "convention"

And to do so you choose your own very personal "convention". You spat on g_magoss and his family.

You shouldn't have done that because you're wrong and g_magoss is right. The convention he mentioned exists (and is not his private or IDE specific) and is used to avoid the problem he pointed out.

g_magossa at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 7
I think we're concentrating on the wrong thing - the original statement was taken the wrong way (though slightly gittish) and it's been apologized for.(I don't much like that convention either.)
DavidKNa at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 8
are you not suppose to do it in recursion ?
bfa0210a at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...
# 9

class fibonocis

{

public static void main(String args[])

{

System.out.println(fib(7));

}

public static int fib(int x)

{

if(x == 1)

{

return 2;

}

else if(x==2)

{

return 3;

}

return (fib(x-1)+fib(x-2));

}

}

here is the code .. it will give you the answer to what ever value you give in your main

bfa0210a at 2007-7-8 23:08:04 > top of Java-index,Java Essentials,New To Java...