Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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]
Comment

array prime numbers

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53]
Comment

how to find prime numbers in an array

#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;	
}
Comment

how to find prime numbers in an array

#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;	
}
Comment

how to find prime numbers in an array

#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;	
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js search json 
Javascript :: regex email javascript 
Javascript :: cypress set timeout for locator 
Javascript :: wait for time javascript 
Javascript :: chart js no points 
Javascript :: javascript detect mobile device 
Javascript :: js queryselector names 
Javascript :: how to reset form after submit using jquery and ajax 
Javascript :: javascript get filename from url 
Javascript :: how to loop audio in js 
Javascript :: js random string 
Javascript :: button click redirection to another page vue 
Javascript :: jquery back button event 
Javascript :: Add click event to querySelectorAll js 
Javascript :: remove all from array that matches 
Javascript :: react ReferenceError: regeneratorRuntime is not defined 
Javascript :: how to get file name in directory node js 
Javascript :: redirect javascript timer 
Javascript :: session storage 
Javascript :: import reactdom 
Javascript :: ajax download file 
Javascript :: NullInjectorError: R3InjectorError httpclient 
Javascript :: react native settimeout 
Javascript :: react allow only numbers in input 
Javascript :: javascript json foreach value 
Javascript :: npx create next app 
Javascript :: ion button transparent 
Javascript :: m- m sequelize 
Javascript :: javascript detect click outside of element 
Javascript :: canvas draw image not blurry 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =