Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
44 views
in C Programming by (20 points)
edited by

Write a program in C to print Fibonacci series using recursion

Please log in or register to answer this question.

1 Answer

0 votes
by (20.3k points)
#include <stdio.h>

// Function to calculate the nth Fibonacci number using recursion
int fibonacci(int n) {
    if (n <= 1) {
        return n;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}

// Function to print the Fibonacci series up to n terms using recursion
void printFibonacci(int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", fibonacci(i));
    }
}

int main() {
    int terms;
    
    printf("Enter the number of terms: ");
    scanf("%d", &terms);
    
    printf("Fibonacci series up to %d terms:\n", terms);
    printFibonacci(terms);
    
    return 0;
}

This program first defines a function fibonacci to calculate the nth Fibonacci number recursively. Then, it defines another function printFibonacci to print the Fibonacci series up to n terms by calling the fibonacci function. Finally, in the main function, it takes input from the user for the number of terms and prints the Fibonacci series up to that number of terms.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...