how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50.
This is a discussion on Calulation prime number, using loops within the C Programming forums, part of the General Programming Boards category; how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50....
how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50.
First figure out how you would calculate a prime number, then put it in a loop that goes 50 times. Once you get those two things working, you can add in a sum calculation inside the while loop. Paper and pencil are your friend.
Well, based on the way you keep asking us for homework solutions without showing any effort yourself, I would guess that you are really asking how we would write a while() loop that calculates the sum of all prime numbers from 1 - 50.
But if you really want to know how you would do it, I would say: by studying while loops, summing (adding) numbers, and how to check if a number is prime. Your textbook, class notes and tutorials (found via Google) are great resources. Then you combine those pieces of knowledge and try to write the program yourself.
If you have trouble, you post your code here (in code tags), with specific questions, and we will help. But we wont give out free solutions, otherwise you wont learn anything.
Here's a solution
Seriously, don't use that. But come up with *something*.Code:#include <stdio.h> unsigned short seed[] = { 509, 508, 506, 504, 500, 498, 494, 492, 488, 482, 480, 474, 470, 468, 464, 458, 452, 450, 444, 440, 438, 432, 428, 422, 414, 410, 408, 404, 402, 398, 384, 380, 374, 372, 362, 360, 354, 348, 344, 338, 332, 330, 320, 318, 314, 312, 300, 288, 284, 282 }; int main(void) { int i, sum = 0; for (i = 0; i < 50; i++) sum += seed[i]^511; printf("Sum of first 50 primes is %d\n", sum); return 0; }
No one ever searches the forum clearly....~80% of the questions on this forum involve something about prime numbers