Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

Find all the prime numbers in the array

var numArray = [2, 3, 4, 5, 6, 7, 8, 9, 10]

numArray = numArray.filter((number) => {
  for (var i = 2; i <= Math.sqrt(number); i++) {
    if (number % i === 0) return false;
  }
  return true;
});

console.log(numArray);
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
Javascript :: singleton class in js 
Javascript :: how to set width 100 react native 
Javascript :: nextjs apollo 
Javascript :: lodash sum array of objects 
Javascript :: react upload image 
Javascript :: req.body showing undefined 
Javascript :: how to pause settimeout in javascript 
Javascript :: javascript timer countdown with seconds 59 
Javascript :: how to convert string to reverse title case in javascript 
Javascript :: insertmany 
Javascript :: ref.current.selectionStart 
Javascript :: p cannot appear as a descendant of p react 
Javascript :: ternary operator multiple conditions 
Javascript :: javascript Update Values of Properties 
Javascript :: sign changely api 
Javascript :: javascript AutoCorrection in Date Object 
Javascript :: JavaScript Object Prototypes 
Javascript :: disable SerializableStateInvariantMiddleware and ImmutableStateInvariantMiddleware middlewares for large redux stores 
Javascript :: javascript addall 
Javascript :: Create & Download PDF from Byte[] Array using jQuery AJAX 
Javascript :: change y scale phaser 
Javascript :: phaser wrap in camera bounds 
Javascript :: get lat long react native 
Javascript :: template literal inside a key in react 
Javascript :: declare 2 d vector js 
Javascript :: javascript list all elements in set 
Javascript :: what is so called abstractions in javascript 
Javascript :: how to make a string in javascript 
Javascript :: localstorage in next js 
Javascript :: grid in js 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =