Monday, January 5, 2015

c using Fibonacci Number and Array of Numbers using Recursion and Element in Array



Find the Nth Fibonacci Number using Recursion

#include 
#include 
int fibo(int);
int main()
{
    int num;
    int result;
    printf("Enter the nth number in fibonacci series: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Fibonacci of negative number is not possible.\n");
    }
    else
    {
        result = fibo(num);
        printf("The %d number in fibonacci series is %d\n", num, result);
    }
    return 0;
}
int fibo(int num)
{
    if (num == 0)
    {
        return 0;
    }
    else if (num == 1)
    {
        return 1;
    }
    else
    {
        return(fibo(num - 1) + fibo(num - 2));
    }
}

Output
 
Enter the nth number in fibonacci series: 8
The 8 number in fibonacci series is 21
 

Find the Biggest Number in an Array of Numbers using Recursion

#include 
#include  
int large(int[], int, int);
int main()
{
    int size;
    int largest;
    int list[20];
    int i;
    printf("Enter size of the list:");
    scanf("%d", &size);
    printf("Printing the list:\n");
    for (i = 0; i < size ; i++)
    {
        list[i] = rand() % size;
        printf("%d\t", list[i]);
    }
    if (size == 0)
    {
        printf("Empty list\n");
    }
    else
    {
        largest = list[0];
        largest = large(list, size - 1, largest);
        printf("\nThe largest number in the list is: %d\n", largest);
    }
}
int large(int list[], int size, int largest)
{
    if (size == 1)
        return largest;
 
    if (size > -1)
    {
        if (list[size] > largest)
        {
            largest = list[size];
        }
        return(largest = large(list, size - 1, largest));
    }
    else
    {
        return largest;
    }
}

Output
 
Enter size of the list:8
Printing the list:
7          6          1          3          1          7          2          4
The largest number in the list is: 7

Recursion to Search an Element in Array

#include 
#include 
int search(int [], int, int);
int main()
{
    int size, index, key;
    int list[20];
    int count = 0;
    int i;
    clrscr();
    printf("Enter the size of the list: ");
    scanf("%d", &size);
    index = size;
    printf("Printing the list:\n");
    for (i = 0; i < size; i++)
    {
        list[i] = rand() % size;
        printf("%d\t", list[i]);
    }
    printf("\nEnter the key to search: ");
    scanf("%d", &key);
    while (index > 0)
    {
        index = search(list, index - 1, key);
        printf("Key found at position: %d\n", index + 1);
        count++;
    }
    if (!count)
        printf("Key not found.\n");
    return 0;
}
int search(int array[], int size, int key)
{
    int location;
    if (array[size] == key)
    {
        return size;
    }
    else if (size == -1)
    {
        return -1;
    }
    else
    {
        return (location = search(array, size - 1, key));
    }
}

Output
 
Enter the size of the list: 10
Printing the list:
3          6          7          5          3          5          6          2          9          1
Enter the key to search: 5
Key found at position: 6
Key found at position: 4

No comments:

Post a Comment