0% found this document useful (0 votes)
8 views4 pages

Recursive Functions in C Programming

The document contains three programs demonstrating recursive functions in C. The first program calculates the factorial of a number, the second generates the Fibonacci series, and the third implements the Ackermann function. Each program includes the necessary code and a main function to execute the respective recursive operations.

Uploaded by

jeetamadhar5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Recursive Functions in C Programming

The document contains three programs demonstrating recursive functions in C. The first program calculates the factorial of a number, the second generates the Fibonacci series, and the third implements the Ackermann function. Each program includes the necessary code and a main function to execute the respective recursive operations.

Uploaded by

jeetamadhar5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab 10: WAP to perform Recursive functions.

10.1. WAP to find factorial of an number.


#include <stdio.h>

int fact(int n) {

// BASE CONDITION
if (n == 0)
return 1;

return n * fact(n - 1);


}

int main() {
printf("Factorial of 5 : %d\n", fact(5));
return 0;
}

10.2. WAP to perform Fibonacci series


#include <stdio.h>

// Function for fibonacci


int fib(int n)
{
// Stop condition
if (n == 0)
return 0;

// Stop condition
if (n == 1 || n == 2)
return 1;

// Recursion function
else
return (fib(n - 1) + fib(n - 2));
}

// Driver Code
int main()
{
// Initialize variable n.
int n = 5;
printf("Fibonacci series "
"of %d numbers is: ",
n);

// for loop to print the fibonacci series.


for (int i = 0; i < n; i++) {
printf("%d ", fib(i));
}
return 0;
}
10.3 WAP to perform Ackerman Functions.
#include <stdio.h>
int ackermann(int m, int n) {
if (m == 0) {
return n + 1;
} else if (m > 0 && n == 0) {
return ackermann(m - 1, 1);
} else if (m > 0 && n > 0) {
return ackermann(m - 1, ackermann(m, n - 1));
}
}
int main() {
int m = 1;
int n = 2;
int result = ackermann(m, n);
printf("A(%d, %d) = %d\n", m, n, result);
return 0;
}

You might also like