C program to check prime numbers in an array

C program to check prime numbers in an array

This is the example of c-programming. You can edit and compile code in your own way on clicking button. If you have better solution or you found any error on code, then give us suggestion.

Full Code:

Code Explanation :

In this example, we are going to learn to prime number in array. We have declared an array with certain prime and non-prime numbers, then print the elements with the message 'prime' and 'Not prime'.

We have to check whether number is prime or not from given single dimension array.


    Array elements are: 
    120, 200, 35, 13, 7, 10, 3, 90

    120 - Not Prime
    200 - Not Prime
     35 - Not Prime
     13 - Prime
     7 - Prime
     10 - Not Prime
     3 - Prime
     90 - Not Prime


  • We have declared an array (arr) with the elements: 120, 200, 35, 13, 7, 10, 3, 90
  • To test the prime numbers, we declare an isPrime() function returning 1, if number is prime and returning 0 if number is not prime.
  • Then we have used a loop with 0 to len-1 (total number of array elements) and call isPrime() by moving array elements one by one (arr[loop]) in main() function.
  • Then we check a print statement condition: (isPrime(arr[loop])?"Prime":"Not Prime")). This return "Prime", if isPrime(arr[loop]) returns 1 and return "Not Prime", if isPrime(arr[loop]) returns 0.

This is the main concept of this program.

If you want to do smae problem without using function then it is very easy.

#include <stdio.h>
int isPrime(int);

int main()
	int loop, i, flag; //loop counter
	//declaring array with prime and not prime numbers
	int arr[]={100, 200, 31, 13, 97, 10, 20, 11};
	//calculate length of the array
	int len = sizeof(arr)/sizeof(arr[0]);
	//print array elements with message 
	//"prime" or "Not prime"
	for(loop=0; loop < len; loop++)
		flag = 0;
		for(i=2; i < arr[loop] / 2; i++)
			if(arr[loop] % i ==0)
				flag = 1;
		//flag is 1, if number is not prime
		if(flag == 1){
			printf("%3d - Not Primen",arr[loop]);
			printf("%3d - Primen",arr[loop]);
	return 0;	

The algorithm and flowchart of this progrem is:


STEP 1 : Start;

STEP 2 : Declare variable arr[], loop, i and flag;

STEP 3 : Initialize arr[] = {100, 200, 31, 13, 97, 10, 20, 11};

STEP 4 : Get the length of array;

STEP 4 : Repeat this process until loop < len
        STEP 4.1 : flag = 0
        STEP 4.2 : Repeat this process until i < arr[loop]
                STEP 4.2.1 : flag = 1
                STEP 4.2.2 : break
        STEP 4.3 : IF flag == 1 THEN
                    Print prime
                    Print not prime
        STEP 4.4 : loop++

STEP 5 : End


C program to check prime numbers in an array

If you got any problem on understanding this example then please comment below.

Our example mainly covers following topics in any programing

  • Basic Programs
  • Number Programs
  • Array Programs
  • Matrix Programs
  • Pattern Programs
  • String Programs
  • Tree Programs
  • Singly Linked List Programs
  • Circular Linked List Programs
  • Doubly Linked List Programs

Here we share programs on various topics such as array strings, series area & volume of geometric figures, mathematical calculation algorithms for sorting & searching and much more.Our goal is to provide you with the perfect solution to all the programming problems you may have encountered either during interviews or in class assignments. If you don’t find what you are looking for then please drop a line in the comment section of example page or you can request from account page so that we can get it added to the below collection of programs. Happy Learning!!

If you are confident with the above programs and are able to successfully understand and run them without any problems then it is time for you to take a step further and learn comprehensive programming principles using examples and flow diagrams. Here is the link: Programming tutorial lists .

If you find this example helpful, don't forget to share it with your friends. And stay updated with us by subscribing adzetech.

If you find any code error or grammetical error then you can suggest for example improvement on clicking below button.

Leave comment

Comments (1)


What are you searching for....?


Subscribe us on youtube to get our videos updates firstly on youtube.


To receive updates on the latest news, tutorials and special offers, subscribe to our email newsletter today!


We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine