C Program to Print Fibonacci Series Using Recursion

The Fibonacci series is a sequence of numbers in which each number is obtained by adding the previous two numbers. The series generally starts with 0 and 1.

फिबोनाची सीरीज संख्याओं का एक ऐसा क्रम है जिसमें प्रत्येक अगली संख्या पिछली दो संख्याओं को जोड़कर प्राप्त की जाती है। यह सीरीज सामान्यतः 0 और 1 से शुरू होती है।

Fibonacci Series:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Formula:

fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)

C Program

The following program uses a recursive function named fib() to calculate and print the first N terms of the Fibonacci series.

निम्नलिखित प्रोग्राम फिबोनाची सीरीज के पहले N पदों की गणना तथा उन्हें प्रिंट करने के लिए fib() नामक रिकर्सिव फंक्शन का उपयोग करता है।

#include <stdio.h>
unsigned long long fib(unsigned int x)
{
if (x == 0)
return 0;
if (x == 1)
return 1;
return fib(x - 1) + fib(x - 2);
}
int main(void)
{
int n, i;
printf("How many terms do you want in Fibonacci series? ");
if (scanf("%d", &n) != 1 || n < 1 || n > 94)
{
printf("Invalid input! Enter a number from 1 to 94.\n");
return 1;
}
printf("Fibonacci Series is:\n");
for (i = 0; i < n; i++)
{
printf("%llu ", fib((unsigned int)i));
}
printf("\n");
return 0;
}

Output

How many terms do you want in Fibonacci series? 10
Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34

Explanation

  1. The fib() function takes an integer argument and returns the corresponding Fibonacci number.

  2. The conditions if (x == 0) and if (x == 1) are the base cases of recursion.

  3. The statement return fib(x - 1) + fib(x - 2); calls the same function twice to calculate the sum of the previous two Fibonacci numbers.

  4. The for loop calls the fib() function repeatedly, starting from 0 and continuing up to N-1.

  5. The program prints the first N terms of the Fibonacci series.

  6. fib() फंक्शन एक पूर्णांक आर्गुमेंट लेता है तथा उसके अनुसार फिबोनाची संख्या वापस करता है।

  7. if (x == 0) तथा if (x == 1) रिकर्सन की बेस कंडीशन हैं।

  8. return fib(x - 1) + fib(x - 2); स्टेटमेंट पिछले दो फिबोनाची पदों का योग निकालने के लिए उसी फंक्शन को दो बार कॉल करता है।

  9. for लूप fib() फंक्शन को बार-बार कॉल करता है। यह 0 से शुरू होकर N-1 तक चलता है।

  10. प्रोग्राम फिबोनाची सीरीज के पहले N पदों को प्रिंट करता है।

Important Points

  • The first two terms of the Fibonacci series are 0 and 1.

  • Every subsequent term is the sum of the previous two terms.

  • The recursive function uses two recursive calls for each non-base-case calculation. Therefore, it is an example of tree recursion.

  • This simple recursive approach performs many repeated calculations and becomes very slow for larger values of N.

  • The program accepts up to 94 terms because the 94th term is the largest term that fits in the standard minimum range of unsigned long long.

  • फिबोनाची सीरीज के पहले दो पद 0 और 1 होते हैं।

  • प्रत्येक अगला पद पिछले दो पदों का योग होता है।

  • रिकर्सिव फंक्शन प्रत्येक सामान्य गणना में दो रिकर्सिव कॉल करता है। इसलिए यह ट्री रिकर्सन का उदाहरण है।

  • यह साधारण रिकर्सिव तरीका कई गणनाएँ बार-बार करता है और N का मान अधिक होने पर बहुत धीमा हो जाता है।

  • प्रोग्राम में अधिकतम 94 पद स्वीकार किए गए हैं क्योंकि 94वाँ पद unsigned long long की मानक न्यूनतम क्षमता में आने वाला सबसे बड़ा पद है।

Note: This program demonstrates recursion for learning purposes. For efficiently generating a large Fibonacci series, an iterative method or memoization can be used to avoid repeated calculations.

नोट: यह प्रोग्राम सीखने के उद्देश्य से रिकर्सन को प्रदर्शित करता है। बड़ी फिबोनाची सीरीज को अधिक कुशलता से बनाने के लिए बार-बार होने वाली गणनाओं से बचने हेतु इटरेटिव मेथड या मेमोइजेशन का उपयोग किया जा सकता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025