Program to print Fibonacci series of given range using recursion


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Write a program to print Fibonacci series of given range using recursion
// Ex: 5->0 1 1 2 3 5; 26->0 1 1 2 3 5 8 13 21

 
#include <stdio.h>
static int sum=0;
main(){
    int range;
    int a=0,b=1;    
    printf("Enter a number:");
    scanf("%d",&range);
    printf("Fibonacci Series: %d %d ",a,b);
    fibonacci(range,a,b);                   // calling function
 
}
int fibonacci(int range,int a,int b){
    
    sum = a+b;
    if(sum>range)
        return 0;
    else{
        printf("%d ",sum);
        fibonacci(range,b,sum);
    }
}

No comments:

Post a Comment