S2-C-Programs

All major conceptual S2 C-Programs with Algorithms

View the Project on GitHub trulyPranav/S2-C-Programs

Code:

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

int main() {
    int a[100], flag, n, i, j;
    printf("Enter the Size of the array: ");
    scanf("%d", &n);
    printf("Enter the array elements: ");
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    for(i = 0; i < n; i++) { // This for loop goes each element by element
        flag = 0;
        if(a[i] <= 1) {
            // Since numbers less than & equal to 1 are not prime
            printf("%d is not a Prime Number\n", a[i]);
        }
        else if(a[i] == 2) {
            printf("%d is a Prime Number\n", a[i]);
        }
        else {
            for(j = 2; j <= sqrt(a[i]); j++) {
                // This for loop checks the prime number condition for the 
                // current a[i] element we are in, if a[i] > 2
                if(a[i] % j == 0) {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0) {
                printf("%d is a Prime Number\n", a[i]);
            } else {
                printf("%d is not a Prime Number\n", a[i]);
            }
        }
    }
    return 0;
}

Algorithm:

Step 1: Start

Step 2: Initialize a[100], n, i, j, flag

Step 3: Read Size of Array and store it in n

Step 4: Read the elements of the array by initializing a for loop from i=0 till i<n

Step 5: Initialize a for loop from i=0 and repeat the below till i<n

Step 5.1: flag = 0

Step 5.2: Check if a[i] is less than or equal to 1:

5.2.1: Initialize a for loop from j=2 till j<=a[i]/2
            if a[i]%j=0, flag=0 and break the loop
5.2.2: After the loop, check flag value:
            - If flag equals 0, Display 'a[i] is a Prime Number'
            - Otherwise, Display 'a[i] is not a Prime Number'

Step 6: Stop