C Programs

C Programming – Functions


Programs at a Glance

This section lists the programs covered under Functions and Recursion in C. Click on a program title to jump directly to its detailed explanation.

  1. Factorial Using Non-Recursive and Recursive Functions
  2. N\(^{th}\) Term of Fibonacci Series Using Recursion
  3. Compute xy Using Function

1. Factorial Using Non-Recursive and Recursive Functions


Problem Statement

Write a C program to find the factorial of a given number using non-recursive and recursive functions.

Student-Friendly Algorithm
  1. Start the program.
  2. Read an integer n from the user.
  3. Call a non-recursive function to compute n! using a loop.
  4. Call a recursive function to compute n! using self-calls.
  5. Display the factorial value returned by both functions.
  6. Stop the program.
C Program

#include <stdio.h>

/* Non-recursive function */
int fact_nonrec(int n) {
    int i, fact = 1;
    for (i = 1; i <= n; i++)
        fact = fact * i;
    return fact;
}

/* Recursive function */
int fact_rec(int n) {
    if (n == 0)
        return 1;
    else
        return n * fact_rec(n - 1);
}

int main() {
    int n;

    printf("Enter a number:\n");
    scanf("%d", &n);

    printf("Factorial (Non-Recursive) = %d\n", fact_nonrec(n));
    printf("Factorial (Recursive) = %d\n", fact_rec(n));

    return 0;
}
    
Sample Output

Enter a number:
5
Factorial (Non-Recursive) = 120
Factorial (Recursive) = 120
    
Explanation

In the non-recursive version, a for loop multiplies all integers from 1 to n, storing the running product in fact. When the loop ends, fact holds the factorial value and is returned to main().

In the recursive version, the function fact_rec() calls itself with the argument n - 1 until the base case n == 0 is reached. Each pending call multiplies its own n with the result of the next call, building the factorial value step by step during the return phase.

2. N\( ^{th} \) Term of Fibonacci Series Using Recursion


Problem Statement

Write a C program to find the nth term of a Fibonacci series using a recursive function.

Student-Friendly Algorithm
  1. Start the program.
  2. Read an integer n from the user (position in the series).
  3. If n is 0 or 1, directly return n as the Fibonacci value.
  4. Otherwise, compute the nth term as the sum of the (n − 1)th and (n − 2)th terms using recursion.
  5. Display the nth Fibonacci term returned by the recursive function.
  6. Stop the program.
C Program

#include <stdio.h>

int fib(int n) {
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return fib(n - 1) + fib(n - 2);
}

int main() {
    int n;

    printf("Enter the value of n:\n");
    scanf("%d", &n);

    printf("Nth Fibonacci term = %d\n", fib(n));
    return 0;
}
    
Sample Output

Enter the value of n:
7
Nth Fibonacci term = 13
    
Explanation

The Fibonacci sequence is defined such that each term (from the third term onward) is the sum of the previous two terms, with base cases F(0) = 0 and F(1) = 1. The function fib() encodes this definition directly by calling itself for n - 1 and n - 2.

When n becomes 0 or 1, the recursion stops and the base values are returned. These base results are then added together on the way back up the call stack to produce the required nth term of the series.

3. Compute xy Using Function


Problem Statement

Write a C program to compute the value of xy using a user-defined function.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the base value x and exponent y from the user.
  3. Define a function that initializes result = 1.
  4. In a loop that runs y times, multiply result by x each time.
  5. Return result from the function.
  6. In main(), call the function with arguments x and y and display the returned value.
  7. Stop the program.
C Program

#include <stdio.h>

int power(int x, int y) {
    int i, result = 1;
    for (i = 1; i <= y; i++)
        result = result * x;
    return result;
}

int main() {
    int x, y;

    printf("Enter base and exponent:\n");
    scanf("%d %d", &x, &y);

    printf("Result = %d\n", power(x, y));
    return 0;
}
    
Sample Output

Enter base and exponent:
2 5
Result = 32
    
Explanation

The function power() starts with result = 1 and then multiplies it by x repeatedly, exactly y times. This is equivalent to computing x multiplied by itself y times, which matches the mathematical definition of xy.

Writing the power logic in a separate function demonstrates modular programming: main() is responsible for input and output, while power() is responsible only for the computation. This separation of concerns makes programs easier to read, test and reuse.