473,419 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

can anyone explain me fibonacci number program?

36
f[0] = 1;
f[1] = 1;
int x;

System.out.print(f[0] + "\n" + f[1] + "\n");
for(x=2; x<=45; x++)

f[x] = f[x-1] + f[x-2];
System.out.println(f[x]); [/code]
i cant understand that how it hows after for statement.it first print 1,1 and then 2.the thing emm not getting is that how it print 2.iknown that in fibonacci number next number is the sum of previous two number but didnt get that how it print 2 and then 5 and so on.please let me explain.
Jul 29 '10 #1
6 2864
chaarmann
785 Expert 512MB
Just trace your program (and future programs) in a debugger, then you can see easily how it works!

f[x] = f[x-1] + f[x-2];
Now let x=2, then from this fact there follows by simply replacing letter x with 2:
f[2] = f[1] + f[0];
which is:
f[2] = 1 + 1 = 2;
because you defined
f[1]and f[0] as being =1!

Now do the same for x=3, then for x=4 etc. and you see how it works.
Jul 30 '10 #2
ahmee
36
igot all but i didnt understand that when you take x=4 why it give all zero as output.Is reason is this that we didnt find x=2,x=3 first and directly jumps on the x=4 so when
f[4]=f[4-1]+f[4-2]
f[4]=f[3]+f[2]


so there is no value regarding f[3]and f[4] so thats why it is displaying all zeros in the output


thanks in advance
Jul 31 '10 #3
chaarmann
785 Expert 512MB
Yes,that's the reason.

You must first compute f[3] and f[2] and assign a value for both before you go on to compute f[4] !

You can compute these values in a loop or through recursion. If you computed right, you will have then as a result stored in f[2]=2 and f[3]=3.
You got the formula for computing f[4] right, so then f[4]=f[3]+f[2] = 3+2 = 5
Jul 31 '10 #4
ahmee
36
i got q new question:

i used math class methods pow for squaring the value and i used loop to square numbers upto ten.... but i am getting the problem.. here is my code bel0w

for(int i=1,j=1; i<10&&j<10; i++,j++){

System.out.print(Math.pow(i,j));
System.out.print("\n");


and iam getting this output instead of squaring the numbers

1.0
4.0
27.0
256.0
3125.0
46656.0
823543.0
1.6777216E7
Aug 4 '10 #5
chaarmann
785 Expert 512MB
Dear Ahmee, please use a new thread if you have a new question.
If you put it in its own thread instead of appending it to a non-related theme, other people will find it easily.
Also please don't email me privately with additional questions. Also open a new thread for them. I am not responding to private questions, because I am not your private tutor, but will only provide help for everyone for free, that means any problem you have that will be discussed openly will also help others that have the same problem.
Aug 4 '10 #6
TheServant
1,168 Expert 1GB
If you want to square numbers, why do you have j in your for loop?

Instead use:
Expand|Select|Wrap|Line Numbers
  1. for(int i=1; i<10; i++){
  2. System.out.print(Math.pow(i,2));
  3. System.out.print("\n");
What you had was:
1^1
2^2
3^3
...

Instead of:
1^2
2^2
3^2
...
Aug 5 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ranyart Olias | last post by:
Can anyone explain why the <root@hiddenworld.net> portion of this html doesn't render in a browser, but the <20031010152346.GA15353@server.hiddenworld.net> portion does? Here is the html...
2
by: John D | last post by:
can anyone explain why a site would use this in their page <!--<BR>--> Presumably it is hiding the <BR> from ... ??? TIA John
4
by: Chris | last post by:
Hello Could anyone explain why the following: #footer ul { float : left; margin : 2px 0px 7px 28px; padding : 0px; width : 360px;
10
by: Chris | last post by:
Could anyone write a small program to log the Signal-to-Noise figures for a Netgear DG834 router? I have been getting very variable SNR readings - and I would like to collect some evidence to...
13
by: C++fan | last post by:
The following code is for list operation. But I can not understand. Could anyone explain the code for me? /* * List definitions. */ #define LIST_HEAD(name, type) struct name { type...
1
by: Andrew | last post by:
Hello, friends, I am implementing web app security using asp.net 1.1, and I found the following source code from Yahoo! Mail login page: <form method="post"...
1
by: Jia Lu | last post by:
Hello I have a program that can telnet to a host. But I cannot understand from part, can anyone explain it to me? Thank you very much. import sys, posix, time
3
by: Bhuwan Bhaskar | last post by:
Hi, Can anyone explain when to use state server or Sql server to save sessions. Thanks, Bhuwan
1
Eclipse
by: Eclipse | last post by:
G'day all Can anyone explain the difference in the results to me as I don't understand why specifying the directory name in two different ways could give a different answer. In CODE 1 below i...
1
by: wangchengxinyi | last post by:
hello everyone: Is anyone know how to program with telit GM862 GPS in python? i want to program with the telit module, but i have never used it, i even dont know how to download a script, how...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.