Assignment 2B
Assignment 2B
Problem Definition:
Write a C program to print the Fibonacci series using
recursion.
Problem Analysis:
Input: Number of elements the user wants to be printed
in the Fibonacci series.
Output: Fibonacci series.
Algorithm Development:
Flowchart:
Code:
//NAME: ARSHIA SINGH
//ROLL NO: UIT2023808
//BATCH:H1
//TITLE: Printing fibonacci series using recursion
#include<stdio.h>
void fibo(int n)
{
static int n1=0,n2=1,n3;
if(n>0)
{
n3=n1+n2;
printf("%d\n",n3);
n1=n2;
n2=n3;
fibo(n-1);
}
}
int main()
{
int n1=0,n;
printf("Enter number of values you want in the series:\n");
scanf("%d",&n);
printf("The fibonacci series is as follows:");
printf("0\n");
fibo(n-2);
return 0;
}
/* OUTPUT
Enter number of values you want in the series:
10
The fibonacci series is as follows:0
1
2
3
5
8
13
21
34
*/
Using if else:
#include<stdio.h>
int fibo(int n)
{
if (n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return(fibo(n-1) + fibo(n-2));
}
}
int main()
{
int n,i;
/* output
Enter number of values you want in the series:
10
The fibonacci series is as follows:0
1
2
3
5
8
13
21
34
*/