This section lists the programs covered under Functions and Recursion in C. Click on a program title to jump directly to its detailed explanation.
Write a C program to find the factorial of a given number using non-recursive and recursive functions.
n from the user.n! using a loop.n! using self-calls.
#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;
}
Enter a number:
5
Factorial (Non-Recursive) = 120
Factorial (Recursive) = 120
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.
Write a C program to find the nth term of a Fibonacci series using a recursive function.
n from the user (position in the series).n is 0 or 1, directly return n as the Fibonacci value.(n − 1)th and (n − 2)th terms using recursion.
#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;
}
Enter the value of n:
7
Nth Fibonacci term = 13
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.
Write a C program to compute the value of xy using a user-defined function.
x and exponent y from the user.result = 1.y times, multiply result by x each time.result from the function.main(), call the function with arguments x and y and display the returned value.
#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;
}
Enter base and exponent:
2 5
Result = 32
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.