how to find prime numbers in an array in javascript
// how to find prime numbers in an array in javascript
let num = [-1,-2,-3,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
let result = [];
function isPrime(num) {
if(num < 2) return false;
for (let k = 2; k < num; k++){
if(num % k == 0){
return false;
}
}
return true;
}
num.forEach(function (element) {
const item = isPrime(element);
if (item) {
result.push(element);
}
});
console.log(result); // [ 2, 3, 5, 7, 11, 13, 17, 19]
#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
",arr[loop],(isPrime(arr[loop])?"Prime":"Not Prime"));
}
printf("
");
return 0;
}
#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
",arr[loop],(isPrime(arr[loop])?"Prime":"Not Prime"));
}
printf("
");
return 0;
}
#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
",arr[loop],(isPrime(arr[loop])?"Prime":"Not Prime"));
}
printf("
");
return 0;
}