Coding Practice

Take 5 numbers in an array and find all pairs of prime numbers from that array

Find all pair of prime numbers from a array

Sample Input Sample Output
2, 3, 4, 5, 6 2, 3
2, 5
3, 5
Sample Output
Enter 5 element: 2 3 4 5 6
2,3
2,5
3,5
Source Code
#include<stdio.h>

void main()
{
    int i, j, k = 0, isPrime, size = 5, arr[5], primeNum[5];

    printf("Enter 5 element: ");
    for(i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    for(i = 0; i < size; i++)
    {
        if(arr[i] == 2)
        {
            primeNum[k++] = arr[i];
        }
        else
        {
            isPrime = 1;
            for(j = 2; j < arr[i]; j++)
            {
                if(arr[i] % j == 0)
                {
                    isPrime = 0;
                    break;
                }
            }
            if(isPrime == 1)
            {
                primeNum[k++] = arr[i];
            }
        }
    }
    if(k < 2)
    {
        printf("\nPair not possible!");
    }
    for(i = 0; i < ((k * (k+1))/2); i++)
    {
        for(j = i+1; j < k; j++)
        {
            printf("%d,%d\n", primeNum[i], primeNum[j]);
        }
    }
    printf("\n");

}
Sample Output
Enter 5 element: 2 3 5 7 11
2,3
2,5
2,7
2,11
3,5
3,7
3,11
5,7
5,11
7,11

No comments:

Post a Comment

Change Theme
X