| |

VerySource

 Forgot password?
 Register
Search
View: 777|Reply: 3

See what's wrong with this Fibonacci program ~~~ Thank you!

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-17 19:00:02
| Show all posts |Read mode
#include <stdio.h>
#include <math.h>
int main ()
{
  int ri, repeat;
    int i, m, n;
      long f;
long fib (int n);

scanf ("% d",&repeat);
for (ri = 1; ri <= repeat; ri ++) {
scanf ("% d% d",&m,&n);
for (i = 1 ;; i ++) {
f = fib (i);
if (f> = m&&f <= n)
printf ("% ld", f);
else if (f> n)
break;}
Ranch
printf ("\n");
}
}

long fib (int n)
{
int i, s;
if (n == 1)
return 1;
else if (n == 2)
return 1;
else if (n> = 3)
(for (i = 3; i <= n; i ++)
s = fib (i-1) + fib (i-2);}
return s;}
Reply

Use magic Report

0

Threads

10

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-6-21 12:45:02
| Show all posts
int fiboNumber(n)
{
if(n==1||n==2)
return 1;
else return (fiboNumber(n-1)+fiboNumber(n-2));
}

The for loop inside your fib() function is unintelligible (is it the sum of the first n terms??)
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-23 21:30:02
| Show all posts
long fib(int n)
{
int i,s;
if(n==1)
return 1;
else if(n==2)
return 1;
else if(n>=3)
{for(i=3;i<=n;i++)
s=fib(i-1)+fib(i-2);}
return s;}
-----------------------------------------------
Just remove the for loop, you will recurse
              else if(n>=3)
{
s=fib(i-1)+fib(i-2);
return s;
                   }
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-16 01:15:01
| Show all posts
Thank you
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list