answersLogoWhite

0


Best Answer

Borrowing the isPrime function from another answer of mine. Note that this will result in terrible performance for large arrays of numbers. (You should look into one of the sieve algorithms for this)

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

// returns the sum of all prime numbers in nums

int findPrimeSum(const int numsLength, const int[] nums) {

int sum = 0;

// iterate through nums and add up all the primes

int i;

for(i = 0; i < numsLength; ++i) {

if( isPrime(nums[i] ) {

sum += nums[i];

}

}

return sum;

}

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a C program to find the sum of all prime numbers in an array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


How do you write a program in objective c numbers 1-100 prime numbers?

fdsgfhgdfhgdf


Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


Write a C program to find the sum of all prime numbers?

Since there is an infinite set of prime numbers the answer would be infinity.


How do you write a C plus plus program that will display the first 10 positive prime numbers?

By learning how to program on C+.


Consider prime numbers between 1 and 10000. Draw a flowchart to display the prime numbers.Write a pseudo code associated with the flowchart in a)?

A) Here's an example of a flowchart and pseudocode that could be used to display the prime numbers between 1 and 10000: Flowchart:
START Set up an array of numbers from 1 to 10000 Set an empty array to store the prime numbers Set i = 2, the first prime number For each number in the array, check if it is divisible by i If it is divisible by i, it is not a prime number and move to the next number in the array If it is not divisible by i, it is a prime number and add it to the prime numbers array Increase i by 1 and go back to step 4 Repeat steps 4 through 7 until i is greater than the square root of 10000 Display the prime numbers array END


What is the term used to describe numbers that can only make 1 array?

They are prime numbers.


How do you write a program to print prime numbers from 1 to 100 in PHP?

&lt;pre&gt; &lt;?php $limit = 100; $primeList = array(); for($i = 2; $i &lt; round(sqrt($limit)); $i++) { foreach($primeList as $num) { if($i%$num 0)) continue 2; } $prime[] = $i; } print_r($prime); ?&gt; &lt;/pre&gt; Based on Sieve of Erathosthenes. See the related link for further information.


How can I write a program to display prime numbers from 1 to 100?

Write a function that implements an algorithm that checks to see if a particular integer is prime (returning a boolean). Write a program that uses that function on each number from 1 to 100, and if true, displays that number.


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


How are all the area model of the prime numbers similar?

They only have one array.


How do you write a c program to get a range from user and give a list of prime numbers?

To get all tutorials of "c programming" Reference:cprogramming-bd.com/c_page2.aspx# prime number