In main(),declare a variable N, input an integer (positive and less than 30) fro
ID: 3666739 • Letter: I
Question
In main(),declare a variable N, input an integer (positive and less than 30) from standard input (keyboard); based on the value of N, use a loop to calculate the N^th Fibonacci number and print the result. Define a recursive function to calculate Fibonacci numbers. In main(), assign N with an integer (positive and less than 20) from standard input. Based on the value of N, call the recursive function and output the N^th Fibonacci number: Fibonnacci(N) = Fibonacci(N - 1) + Fibonacci(N-2) The following specifications will be expected for each of your projects in this class. An empty project file must be created first, and then you will create new source file and header files. Add comments at the beginning of the program, and add description for each function, loop, etc.Explanation / Answer
a.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1,b=1,c,no,i;
clrscr();
printf(" Enter No in Series");
scanf("%d",&no);
printf(" %d",a);
printf(" %d",b);
for(i=1;i<=no-2;i++)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
getch();
}
b.
#include<stdio.h>
void printFibonacci(int);
void fibo(int);
int main()
{
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
fibo(n);
return 0;
}
void printFibonacci(int n){
static long int f0=1,f1=1,f2=2;
if(n>0){
f2 = f0 + f1;
f0 = f1;
f1= f2;
printf("%ld ",f2);
printFibonacci(n-1);
}
}
void fibo(int n)
{
int i,f0=1,f1=1,f2=0;
for(i=1;i<=n-2;i++)
{
f2=f0+f1;
printf(" %d",f2);
f0=f1;
f1=f2;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.