Sunday, June 6, 2021

C Array And Recursive Function To Check Prime Number And Largest Number

 Details of the program: Write a C program that takes a 1-dimensional array arr as input (of size 10) and check if each element in arr is prime or not using recursion, and store the prime numbers in another array arr2

Subtract 2 from each element in arr2
Use recursion to find the largest element in arr2

   
   
   #include<stdio.h>
   int x;
   void main()
   {
    int arr1[10],arr2[10];
    int i;
    for(i=0;i<10;i++)
    {
    printf("\nElement No %d: ",(i+1));
    scanf("%d",&arr1[i]);
    }
    for(i=0;i<10;i++)
{

if(isPrime(arr1[i],arr1[i]/2))

arr2[x++]=arr1[i];
}
printf("\nPrime numbers are\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
for(i=0;i<x;i++)
arr2[i]=arr2[i]-2;

printf("\nAfter Deduction\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
i=getMax(arr2);
printf("\nMax Value=%d",i);
getch();
   }
  int isPrime(int n, int i)
{
    if(i == 1)
        return 1;   
    else
    {
        if(n%i == 0)
            return 0;
        else
            isPrime(n, i-1);    
    }
}
  int getMax(int a[])
{
    static int i = 0, max =- 9999;  
    if(i < x)   
    {
        if(max < a[i])
        max = a[i];
        i++;    
        getMax(a);   
    }
    return max;
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner