Like Tree4Likes
  • 1 Post By SirPrattlepod
  • 3 Post By grumpy

Calulation prime number, using loops

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....

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Question Calulation prime number, using loops

    how would I write a while() loop that calculates the sum of all prime numbers from 1 - 50.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    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.

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Here's a solution

    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;
    }
    Seriously, don't use that. But come up with *something*.
    nonpuz likes this.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    No one ever searches the forum clearly....~80% of the questions on this forum involve something about prime numbers

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,172
    Quote Originally Posted by nonpuz View Post
    No one ever searches the forum clearly....~80% of the questions on this forum involve something about prime numbers
    The percentage is not THAT high, but it is certainly not insignificant. And you have demonstrated the phenomenon that 74.6% of statistics are made up on the spot.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding only prime numbers with for loops
    By AlmondSeason in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2013, 11:51 PM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  3. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  4. Price Calulation Method
    By gogo in forum C++ Programming
    Replies: 2
    Last Post: 11-27-2001, 06:47 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21