Monday, July 30, 2018

C PROGRAM TO ARRANGE THE GIVEN NUMBERS IN ORDER


STATEMENT:

A set of numbers of size N which are separated by one or more spaces will be passed as input. The program should print the prime numbers first followed by odd numbers and finally even numbers.
Each of these categories, prime numbers, odd numbers and even numbers must be sorted in ascending order among themselves. The numbers which are prime must be excluded from the list of odd and even numbers (In the case of even numbers only 2 is prime as well as even)

PROGRAM:

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

struct NumberItem
{
    int val;
    int alreadyprinted;
};

int isPrime(int num);

int main()
{
    char input[200];
    fgets(input,200,stdin);
    char *ptr = input;
    int offset=0;
    int currentNumber;

    int  N=0;
    while(sscanf(ptr,"%d%n",&currentNumber,&offset) == 1)
    {
        N++;
        ptr+=offset;
    }

    struct NumberItem values[N];

    int index=0;
    ptr = input;
    while(sscanf(ptr,"%d%n",&currentNumber,&offset) == 1)
    {
        values[index].val = currentNumber;
        values[index].alreadyprinted = 0;
        ptr+=offset;
        index++;
    }

    //Sort the numbers
    index = 0;
    while(index < N-1)
    {
        int compareindex=index+1;
        while(compareindex < N)
        {
            if(values[index].val > values[compareindex].val)
            {
                int temp = values[index].val;
                values[index].val = values[compareindex].val;
                values[compareindex].val = temp;
            }
            compareindex++;
        }
        index++;
    }


    //Now print prime
    index = 0;
    while(index < N)
    {
        if(isPrime(values[index].val))
        {
            printf("%d ",values[index].val);
            values[index].alreadyprinted = 1;
        }
        index++;
    }

    //Now print odd
    index = 0;
    while(index < N)
    {
        if(!values[index].alreadyprinted)
        {
            if(values[index].val%2 == 1)
            {
                printf("%d ",values[index].val);
                values[index].alreadyprinted = 1;
            }

        }

        index++;
    }


    //Now print even
    index = 0;
    while(index < N)
    {
        if(!values[index].alreadyprinted)
        {
            if(values[index].val%2 ==  0)
            {
                printf("%d ",values[index].val);
                values[index].alreadyprinted = 1;
            }

        }

        index++;
    }

}

int isPrime(int num)
{
    if(num < 2)
    {
        return 0;
    }

    if(num == 2)
    {
        return 1;
    }

    int  square_root = (int) sqrt((double)num);
    int counter=2;
    while(counter <= square_root)
    {
        if(num%counter == 0)
        {
            return 0;
        }

        counter++;
    }

    return 1;

}



Input:
611953 494147 493137 493133 493138

Output:
493133 494147 611953 493137 493138

Explanation:
493133 494147 611953 are prime numbers.

No comments:

Post a Comment