Search This Blog
Welcome to 1printf(), your ultimate destination for C, C++, Linux, Data Structures, and Microcontroller programming! 🚀 🔹Learn advanced coding techniques in C& C++ 🔹Master Linux internals & shell scripting 🔹Deep dive into Data Structures & Algorithms 🔹Explore Embedded Systems & Microcontrollers (8051,UART, RTOS) 🔹Get hands-on coding tutorials, project ideas,and interview preparation tips Whether you're a beginner or an experienced programmer, this channel will help you
Featured
- Get link
- X
- Other Apps
Fibonacci Series Using Recursion in C
Fibonacci Series Using Recursion in C
This C program prints the Fibonacci series using recursion. The Fibonacci series is a sequence where each number is the sum of the two preceding ones. It starts from 0 and 1.
✅ C Program Code:
#include <stdio.h>
int fib(int n)
{
if(n == 0)
return 0;
if(n == 1)
return 1;
return fib(n - 1) + fib(n - 2);
}
int main()
{
int num;
printf("Enter the number of terms you want to print:\n");
scanf("%d", &num);
if(num < 0)
{
printf("Fibonacci series is not defined for negative numbers.\n");
return 1;
}
printf("Fibonacci series up to %d terms:\n", num);
for(int i = 0; i < num; i++)
{
printf("%d ", fib(i));
}
printf("\n");
return 0;
}
📌 How It Works:
- fib(): Recursively returns the nth Fibonacci number.
- Input Check: Prevents calculation for negative numbers.
- Loop: Calls fib() from 0 to n-1 and prints the result.
💻 Sample Output:
6
Fibonacci series up to 6 terms:
0 1 1 2 3 5
🔍Keywords:
fibonacci series recursion in C, recursive fibonacci code, print fibonacci numbers in C, C programs for beginners, DSA recursion logic, fib function explanation
Popular Posts
C++ Program for Hybrid Inheritance (All Types Together)
- Get link
- X
- Other Apps
C++ Program for Function Overloading Example
- Get link
- X
- Other Apps
Comments
Post a Comment