Q:

C program to check prime numbers in an array

0

C program to check prime numbers in an array

Given an array of integer elements and we have to check which prime numbers using C program are.

Example:

    Input:
    Array elements are: 
    100, 200, 31, 13, 97, 10, 20, 11

    Output:
    100 - Not Prime
    200 - Not Prime
     31 - Prime
     13 - Prime
     97 - Prime
     10 - Not Prime
     20 - Not Prime
     11 - Prime

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check prime numbers in an array using C program

#include <stdio.h>

//function to check number is prime or not
//function will return 1 if number is prime
int isPrime(int num)
{
	int i; //loop counter
	//it will be 1 when number is not prime
	int flag=0; 
	//loop to check number is prime or not
	//we will check, if number is divisible
	//by any number from 2 to num/2, then it
	//will not be prime
	for(i=2; i<num/2; i++)
	{
		if(num%i ==0)
		{
			flag =1;
			break;
		}
	}
	//flag is 1, if number is not prime
	if(flag==1)
		return 0;
	else
		return 1;
}

int main()
{
	int loop; //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++)
	{
		printf("%3d - %s\n",arr[loop],(isPrime(arr[loop])?"Prime":"Not Prime"));
	}
	
	printf("\n");
	
	return 0;	
}

Output

100 - Not Prime
200 - Not Prime
 31 - Prime
 13 - Prime
 97 - Prime
 10 - Not Prime
 20 - Not Prime
 11 - Prime

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to create array with reverse elements of... >>
<< C program to delete prime numbers from an array...