Skip to main content

Featured

C Program to Check Prime Number Using Efficient Logic

  Introduction A prime number is a number that has exactly two distinct positive divisors: 1 and itself. In this program, we check whether a given number is prime or not using a simple and efficient logic. This type of program is commonly used in mathematics, competitive programming, and basic algorithm learning for beginners in C programming. Problem Statement The task is to write a C program that determines whether a given integer is a prime number or not. The program takes a single integer input from the user and analyzes its divisibility. If the number has no divisors other than 1 and itself, it should be identified as a prime number; otherwise, it is not prime. This problem is important in number theory and has practical relevance in areas such as cryptography, data validation, and algorithm design.  Algorithm / Logic Explanation To check whether a number is prime, we need to verify that it is not divisible by any number other than 1 and itself. The algorithm follows a si...

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

🌙