Given N numbers, write a function that finds out if there is at least one prime among them and returns it, else returns 0.
A naive approach would check each number for prime- ness. Can you do better ?
Answers
Answered by
0
Answer:
#include
int prime(int); // Declera prime function
int main()
int num;
printf(Enter a number?\n); // Get a input number
scanf(%d, &num);
printf(%d\n,prime(num)); // Call the prime funciton
return 0;
/*
Prime numbers is a number which has only two factors 1 and itself.
*/
int prime(int n)
int i,c=0;
if(n<2) if="" n="" is="" 0,1="" return="">
return 0;
for(i=2;i<(n )+1;i++)="" loop="" from="" 2="" to="" (n/2)+1="" as="" n="" cannot="" be="" divisible="" after="">
if(n%i==0) // If n is divisible by i
return 0; // Return 0. (since i is a factor of n)
Similar questions
English,
9 hours ago
Social Sciences,
9 hours ago
Environmental Sciences,
17 hours ago
English,
17 hours ago
Math,
8 months ago
Math,
8 months ago