1. Write a program that finds the prime numbers using sieve method

Solution:

#include <stdio.h>
int main()
{
    int number, i, j;
    printf("Enter the number:\\n");
    scanf("%d", &number);

    int primes[number + 1];
    for (i = 2; i <= number; i++)
        primes[i] = i;

    i = 2;
    while ((i * i) <= number)
    {
        if (primes[i] != 0)
        {
            for (j = 2; j < number; j++)
            {
                if (primes[i] * j > number)
                    break;
                else
                    primes[primes[i] * j] = 0;
            }
        }
        i++;
    }

    for (i = 2; i <= number; i++)
    {
        if (primes[i] != 0)
            printf("%d\\t", primes[i]);
    }
    printf("\\n");
    return 0;
}

input

100

Sample Output:

Enter the number:
2	3	5	7	11	13	17	19	23	29	31	37	41	43	47	53	59	61	67	71	73	79	83	89	97

2. Write a program that finds the largest and smallest elements in an array

Solution:

#include <stdio.h>
int main()
{
    int n;
    printf("Enter the size of array: \\n");
    scanf("%d", &n);
    int array[n];
    printf("Enter the elements of array: ");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }
    int max = array[0], min = array[0];
    for (int i = 0; i < n; i++)

    {
        if (max < array[i])

            max = array[i];

        if (min > array[i])

            min = array[i];
    }
    printf("The Maximum number is: %d\\n", max);
    printf("The Minimum number is: %d\\n", min);

    return 0;
}

input