Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

๐Ÿ”ข C Program to Print Hollow Continuous Number Square ๐Ÿ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } ๐Ÿ“‹ Copy Code ๐Ÿ’ป Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ๐Ÿ”ข C Program to Print Standard Hollow Binary Row Square ๐Ÿ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Fibonacci Series Using Recursion in C

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:

Enter the number of terms you want to print:
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

Comments

Popular Posts

๐ŸŒ™