0% found this document useful (0 votes)
17 views6 pages

Assignment 2B

Uploaded by

arshiasingh9787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Assignment 2B

Uploaded by

arshiasingh9787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT-2(B)

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;

printf("Enter number of terms to be printed:");


scanf("%d",&n);
printf("Fibonaacci series is as follows:");
for (i=0;i<n;i++)
{
printf("%d ",fibo(i));
}
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
*/

You might also like