Problem Description: You are given an array A of n integers. You have to make a queue and stack the given integers. The queue should contain only prime numbers and the stack should contain only composite numbers. All numbers in the array will be > 1 . The rule to form the stack and queue is that you should be able to generate the array using the pop and dequeue operations. Note: Please read this explanation carefully Let the array A contains 5 integers: 7 , 21 , 18 , 3 , 12 then the content of queue and stack will be : Queue : 7 , 3 Stack : 12 , 18 , 21 Now if you follow the rules of stack and queue then you see that you can generate the array using the pop operations of stack and dequeue operation of the queue as follows :
C Program

#include<stdio.h>
#include<math.h>

int top=-1;

/* Mandatory */
int read_int()
{
    int x;
    scanf("%d",&x);
    return x;
}

/* Mandatory */
void push(int stack[],int data)
{
    top++;
    stack[top]=data;
}

int isPrime(int n)
{
    if(n<2) return 0;
    for(int i=2;i*i<=n;i++)
        if(n%i==0) return 0;
    return 1;
}

int main()
{
    int n=read_int();

    int queue[1000000], stack[1000000];
    int qr=0;

    for(int i=0;i<n;i++)
    {
        int x=read_int();

        if(isPrime(x))
            queue[qr++]=x;
        else
            push(stack,x);
    }

    for(int i=0;i<qr;i++)
        printf("%d ",queue[i]);

    printf("\n");

    for(int i=top;i>=0;i--)
        printf("%d ",stack[i]);

    return 0;
}