C fibonacci sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tirantha
    New Member
    • Sep 2010
    • 1

    C fibonacci sequence

    I wanna wright C programe to find value F(n)value with in(0=<n<=20),pr ogramme must be we input n valu then we get out put F(n) value.
    fibonacci sequence
    F(n)=F(n-1)+F(n-2)
    Code:
    #include<stdio.h>
    int main()
    {
    int n,ans=0,n1=0,n2=0,i,p;
    printf("enter n:");
    scanf("%d",&n);
    p=n;
    if(n==0)
    printf("%d",n1);
    if(i==1)
    printf("%d",n2);
    else
    for(i=0;i<p-2;p++)
    {
    ans=n1+n2;
    n1=n2;
    ans=n2;
    }
    printf("%d",ans);
    return 0;
    }
    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..
    Last edited by Banfa; Sep 3 '10, 12:16 PM. Reason: Changed italics to [code] ...[/code] tags
  • haicp
    New Member
    • Sep 2010
    • 4

    #2
    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)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      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).

      Comment

      Working...