0% found this document useful (0 votes)
4 views

CP LAB 12

The document contains several C programs demonstrating the use of recursive functions. It includes implementations for generating the Fibonacci series, finding the least common multiple (LCM), calculating the factorial of a number, implementing the Ackermann function, and summing a series of natural numbers. Each program includes a main function that prompts user input and calls the respective recursive function to perform the desired computation.

Uploaded by

leopasala48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CP LAB 12

The document contains several C programs demonstrating the use of recursive functions. It includes implementations for generating the Fibonacci series, finding the least common multiple (LCM), calculating the factorial of a number, implementing the Ackermann function, and summing a series of natural numbers. Each program includes a main function that prompts user input and calls the respective recursive function to perform the desired computation.

Uploaded by

leopasala48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab - 12: Recursive functions

(i) Aim: Write a recursive function to generate Fibonacci series.

Program:
#include<stdio.h>

int fib(int);

int main()
{
int n, i;
printf("Enter number of terms you want: ");
scanf("%d", &n);
printf("\nThe %d terms of fibonacci series are: \n",n);
for(i = 0; i < n; i++)
printf("%d\t",fib(i));
return 0;
}

int fib(int x)
{
switch(x)
{
case 0:
return 0;
case 1:
return 1;
default:
return (fib(x - 2) + fib(x - 1));
}
}
(ii) Aim: Write a recursive function to find the lcm of two numbers.

Program:
#include <stdio.h>

int lcm(int, int);

int main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
result = lcm(a, b);
printf("The LCM of %d and %d is: %d\n", a, b, result);
return 0;
}

int lcm(int a, int b)


{
static int multiple;
int temp;
if(a == 1 || b == 1)
return a * b;
else if(a == b)
return a;
else
{
multiple++;
if(multiple % a == 0 && multiple % b == 0)
{
temp = multiple;
multiple = 0;
return temp;
}
lcm(a, b);
}
}
(iii) Aim: Write a recursive function to find the factorial of a number.

Program:
#include<stdio.h>

int factorial(int);

int main()
{
int num;
printf("Enter a whole number: ");
scanf("%d", &num);
if(num < 0)
printf("Invalid number.");
else
printf("The factorial of %d is: %d", num, factorial(num));
return 0;
}

int factorial(int n)
{
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
(iv) Aim: Write a C Program to implement Ackermann function using recursion.

Program:
#include<stdio.h>

int ack(int, int);

int main()
{
int m, n;
printf("Enter any two non-negative numbers: ");
scanf("%d %d", &m, &n);
if(m < 0 || n < 0)
printf("Invalid input.");
else
{
printf("Output of Ackermann Function is: \n");
printf("A(%d, %d) = %d", m, n, ack(m, n));
}
return 0;
}

int ack(int m, int n)


{
if(m == 0)
return (n + 1);
else if(n == 0)
return ack(m - 1, 1);
else
return ack(m - 1, ack(m, n - 1));
}
(v) Aim: Write a recursive function to find the sum of series.

Program:
#include<stdio.h>

int sum(int);

int main()
{
int n;
printf("Enter a natural number: ");
scanf("%d", &n);
if(n < 0)
printf("Invalid number...");
else
printf("Sum of the first %d natural numbers is: %d", n, sum(n));
return 0;
}

int sum(int num)


{
if(num)
return (num + sum(num - 1));
else
return 0;
}

You might also like