By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 IT Pros & Developers. It's quick & easy.

C fibonacci sequence

P: 1
I wanna wright C programe to find value F(n)value with in(0=<n<=20),programme must be we input n valu then we get out put F(n) value.
fibonacci sequence
F(n)=F(n-1)+F(n-2)
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n,ans=0,n1=0,n2=0,i,p;
  5. printf("enter n:");
  6. scanf("%d",&n);
  7. p=n;
  8. if(n==0)
  9. printf("%d",n1);
  10. if(i==1)
  11. printf("%d",n2);
  12. else
  13. for(i=0;i<p-2;p++)
  14. {
  15. ans=n1+n2;
  16. n1=n2;
  17. ans=n2;
  18. }
  19. printf("%d",ans);
  20. return 0;
  21. }
but i got this error
Building fib.exe.
POLINK: fatal error: Access is denied.
*** Error code: 1 ***
Done.


plz find the error in this progm..
Sep 3 '10 #1
Share this Question
Share on Google+
2 Replies


P: 4
This program compiles perfectly. But goes into an infinite loop.

The problem is,
a) the value of i is not initialised to execute if(i==1) condition
b) the for(i=0;i<p-2;p++) loop never breaks for any value > 2
c) if the for loop is changed to for(i=0;i<p-2;i++) then this will not be an infinite loop
d) even then the value of ans will be zero because the values of n1 & n2 are never changed (updated)
Sep 3 '10 #2

Banfa
Expert Mod 5K+
P: 8,916
Following haicps observation that the program enters an infinite loop the reason for the "Access is Denied" is most likely that the program is still running from when you previously tested it. On windows while running programs executable file is locked so the linker can not open it to write the new output.

This error is not caused by an error in the code (which does not equate to the code being error free see haicps post).
Sep 3 '10 #3

Post your reply

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