Monday, October 27, 2014

Number is Prime or Not using Recursion

Find whether a Number is Prime or Not using Recursion

#include 
#include 
int primeno(int, int);
 
int main()
{
    int num, check;
    clrscr();
    printf("Enter a number: ");
    scanf("%d", &num);
    check = primeno(num, num / 2);
    if (check == 1)
    {
        printf("%d is a prime number\n", num);
    }
    else
    {
        printf("%d is not a prime number\n", num);
    }
    return 0; 
    
}
 
int primeno(int num, int i)
{
    if (i == 1)
    {
        return 1;
    }
    else
    {
       if (num % i == 0)
       {
         return 0;
       }
       else
       {
         return primeno(num, i - 1);
       }       
    }
    getch();
}

Output
 
Enter a number: 456
456 is not a prime number
 
Enter a number: 89
89 is a prime number


Find the Factorial of a Number using Recursion

#include 
#include 
int factorial(int);
 
int main()
{
    int num;
    int result;
    clrscr();
    printf("Enter a number to find it's Factorial: ");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Factorial of negative number not possible\n");
    }
    else
    {
        result = factorial(num);
        printf("The Factorial of %d is %d.\n", num, result);
    }
    return 0;
}
int factorial(int num)
{
    if (num == 0 || num == 1)
    {
        return 1;
    }
    else
    {
        return(num * factorial(num - 1));
    }
}

Output
 
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.

Find LCM of a Number using Recursion

#include 
#include 
int lcm(int, int);
 
int main()
{
    int a, b, result;
    int prime[100];
    clrscr();
    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 common = 1;
 
    if (common % a == 0 && common % b == 0)
    {
        return common;
    }
    common++;
    lcm(a, b);
    return common;
}

Output
 
Enter two numbers: 456
12
The LCM of 456 and 12 is 456
 
Enter two numbers: 45 75
The LCM of 45 and 75 is 225

Find Product of 2 Numbers using Recursion

#include 
#include 
int product(int, int);
 
int main()
{
    int a, b, result;
    clrscr();
    printf("Enter two numbers to find their product: ");
    scanf("%d%d", &a, &b);
    result = product(a, b);
    printf("Product of %d and %d is %d\n", a, b, result);
    return 0;
}
 
int product(int a, int b)
{
    if (a < b)
    {
        return product(b, a);
    }
    else if (b != 0)
    {
        return (a + product(a, b - 1));
    }
    else
    {
        return 0;
    }
}

Output
 
Enter two numbers to find their product: 176 340
Product of 176 and 340 is 59840

Check whether a given String is Palindrome or not using Recursion

#include 
#include 
#include 
 
void check(char [], int);
 
int main()
{
    char word[15];
    clrscr();
    printf("Enter a word to check if it is a palindrome\n");
    scanf("%s", word);
    check(word, 0);
 
    return 0;
}
 
void check(char word[], int index)
{
    int len = strlen(word) - (index + 1);
    if (word[index] == word[len])
    {
        if (index + 1 == len || index == len)
        {
            printf("The entered word is a palindrome\n");
            return;
        }
        check(word, index + 1);
    }
    else
    {
        printf("The entered word is not a palindrome\n");
    }
}
}

Output
 
Enter a word to check if it is a palindrome
malayalam
The entered word is a palindrome

Find GCD of given Numbers using Recursion

#include 
#include  
int gcd(int, int);
 
int main()
{
    int a, b, result;
 
    printf("Enter the two numbers to find their GCD: ");
    scanf("%d%d", &a, &b);
    result = gcd(a, b);
    printf("The GCD of %d and %d is %d.\n", a, b, result);
}
 
int gcd(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return gcd(a - b, b);
        }
        else
        {
            return gcd(a, b - a);
        }
    }
    return a;
}
}

Output
 
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.



Perform Binary Search using Recursion

#include 
#include 
void binary_search(int [], int, int, int);
void bubble_sort(int [], int);
 
int main()
{
    int key, size, i;
    int list[25];
    clrscr();
    printf("Enter size of a list: ");
    scanf("%d", &size);
    printf("Generating random numbers\n");
    for(i = 0; i < size; i++)
    {
        list[i] = rand() % 100;
        printf("%d  ", list[i]);
    }
    bubble_sort(list, size);
    printf("\n\n");
    printf("Enter key to search\n");
    scanf("%d", &key);
    binary_search(list, 0, size, key);
 }
 void bubble_sort(int list[], int size)
{
    int temp, i, j;
    for (i = 0; i < size; i++)
    {
        for (j = i; j < size; j++)
        {
            if (list[i] > list[j])
            {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
    }
}
 
void binary_search(int list[], int lo, int hi, int key)
{
    int mid;
 
    if (lo > hi)
    {
        printf("Key not found\n");
        return;
    }
    mid = (lo + hi) / 2;
    if (list[mid] == key)
    {
        printf("Key found\n");
    }
    else if (list[mid] > key)
    {
        binary_search(list, lo, mid - 1, key);
    }
    else if (list[mid] < key)
    {
        binary_search(list, mid + 1, hi, key);
    }
}
}

Output
 
Enter size of a list: 10
Generating random numbers
83  86  77  15  93  35  86  92  49  21  
 
Enter key to search
21
Key found

Print Binary Equivalent of an Integer using Recursion

#include 
#include  
int binary_conversion(int);
 
int main()
{
   int num, bin;
   clrscr();
   printf("Enter a decimal number: ");
   scanf("%d", &num);
   bin = binary_conversion(num);
   printf("The binary equivalent of %d is %d\n", num, bin);
}
 
int binary_conversion(int num)
{
    if (num == 0)
    {
        return 0;
    }
    else
    {
        return (num % 2) + 10 * binary_conversion(num / 2);
    }
}
}

Output
 
Enter a decimal number: 10
The binary equivalent of 10 is 1010

Print the Alternate Nodes in a Linked List using Recursion

#include 
#include 
#include 
 
struct node
{
    int a;
    struct node *next;
};
 
void generate(struct node **);
void display(struct node *);
void delete(struct node **);
 
int main()
{
    struct node *head = NULL;
 
    generate(&head);
    printf("\nDisplaying the alternate nodes\n");
    display(head);
    delete(&head);
 
    return 0;
}
 
void display(struct node *head)
{
    static flag = 0;
    if(head != NULL)
    {
        if (!(flag % 2))
        {
           printf("%d  ", head->a);
        }
        flag++;
        display(head->next);
    }
}
 
void generate(struct node **head)
{
    int num, i;
    struct node *temp;
 
    printf("Enter length of list: ");
    scanf("%d", &num);
    for (i = num; i > 0; i--)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = i;
        if (*head == NULL)
        {
            *head = temp;
            (*head)->next = NULL;
        }
        else
        {
            temp->next = *head;
            *head = temp;
        }
    }
}
 
void delete(struct node **head)
{
    struct node *temp;
    while (*head != NULL)
    {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Output
 
Enter length of list: 10
 
Displaying the alternate nodes
1  3  5  7  9


No comments:

Post a Comment