All major conceptual S2 C-Programs with Algorithms
#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;
}
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'