BCA

C AND LINEAR DATA STRUCURE

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6

PART C

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 . .

8. Program to implement any three recursive functions.

 
  
// recursive functions

#include < stdio.h >
int sum(int n);
int main() {

int number, result;
int ch;
printf("Enter choice 1.sum of integer 2.factorial  3.fibonacci ");

switch(ch){

case 1:
    printf("Enter a positive integer: ");
    scanf("%d", &number);
    result = sum(number);
    printf("\nsum = %d", result);
break;

case 2:
    printf("Enter a   integer: ");
    scanf("%d", &number);
    result = factorial(number);
    printf("\nfactorial = %d", result);

break;

case 3:
    printf("Enter a   integer: ");
    scanf("%d", &number);
    result = fibonacci(number);
    printf("\nfibonacci = %d", result);

break; 
   return 0;



}
int sum(int n) {
    if (n != 0)
        // sum() function calls itself
        return n + sum(n-1); 
    else
        return 0;
 }

int factorial(int n)
{
    if(n > 1)
        return n * factorial(n - 1);
    else
        return 1;
}

int fibonacci(int n) 
{ 
    if (n <= 1) 
        return n; 
    return fib(n-1) + fib(n-2); 
}