I can't get this program to run properly

Joined
Dec 17, 2022
Messages
1
Reaction score
0
#include"stdio.h"
#include"conio.h"
int fibo(int);
int main()
{
int a=0;
while(a!=10)
{
printf(" %d",fibo(a));
a++;
}
return 0;
}
int fibo(int a)
{
if(a==0)
{
return 0;
}
else if(a==1)
{
return 1;
}
else
{
return (fibo(a-1)+fibo(a-2));
}
}


This program calculates the Fibonacci series and displays it . But it displays the total sum of the series rather than the individual components of the series
 
Joined
Dec 18, 2022
Messages
1
Reaction score
0
A Fibonacci number is defined as the sum of the two preceding numbers. I am not sure what you want exactly.
 
Joined
Jun 29, 2022
Messages
28
Reaction score
0
The provided program correctly calculates and prints the individual terms of the Fibonacci sequence using a recursive function named fibo() and a loop in the main() method. However, the program's output only shows the individual terms of the series, not the whole sum of the series.

To display the overall sum of the series, make the following changes to the main() function:
Code:
int main()
{
    int a=0, sum=0;
    while(a!=10)
    {
        int term = fibo(a);
        sum += term;    // add each term to the running total
        printf(" %d",term);
        a++;
    }
    printf("\nTotal sum of the Fibonacci series: %d", sum);
    return 0;
}
A new variable named sum is introduced to this updated program to keep track of the series' running total. Before printing the term, each term in the series is added to the total variable within the loop. The whole amount is written using a printf() command at the end of the loop.
I hope this helps to solve your question. If you don't understand how it works, read this article, which has an example comparable to yours.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top