Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
107 views
in C++ by (178k points)
Can you provide an example of a recursive function to calculate the nth Fibonacci number in C++?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Certainly! Here's an example:

#include <iostream>

int fibonacci(int n) {
    // Base case: fibonacci of 0 or 1 is the number itself
    if (n == 0 || n == 1) {
        return n;
    }
    // Recursive case: fibonacci of n is the sum of fibonacci(n-1) and fibonacci(n-2)
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    int num = 6;
    int result = fibonacci(num);
    std::cout << "Fibonacci number at position " << num << " is " << result << std::endl;
    return 0;
}
 

Output:

Fibonacci number at position 6 is 8
 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jun 7, 2023 in C++ by kvdevika (178k points)
0 votes
1 answer
asked Jun 7, 2023 in C++ by kvdevika (178k points)

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

...