Thread: C++ prime number loop?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    C++ prime number loop?

    I am still very new to c++ and i am totally confused on loops so can anyone help me write a program to
    "Write a program that finds and prints all prime numbers less than or equal to n, where n is a number input by the user."

    This is the template were supposed to use

    Code:
    #include <iostream>
    
    using namespace std;
    
    // ======================
    // main function
    // ======================
    int main()
    {
    
    // Variable declarations
    int n, i, k;
    int counter = 0;
    bool isprime;
    
    //Welcome user to the program
    
    
    
    //Prompt user for an integer
    
    
    
    
    // Outer loop, tests to see if k is prime for k=2 to n
    
    {
    // Assume k is prime
    
    
    // Inner loop tests whether or not k is prime
    
    {
    
    
    
    }
    // Check if flag isprime is still true
    
    
    }
    
    //Output total number of primes found
    
    
    }

    Our output is supposed to be
    Program to compute prime numbers less than n.

    Enter a positive integer n: 10

    2 3 5 7
    There are 4 primes less than 10.

    [email protected]~/cs1500/lab3) a.out


    Program to compute prime numbers less than n.

    Enter a positive integer n: 50

    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
    There are 15 primes less than 50.


    Thanks sooooooo much
    Last edited by Salem; 10-08-2010 at 11:49 AM. Reason: Added code tags - it didnīt help

  2. #2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you know what a prime number is?

    Do you know how to test any given number to see if it is prime or not?

    Have you googled anything along the lines of "prime number algorithm" (like Xupicor has)?


    Maybe start with
    Code:
    for ( i = 1 ; i < n ; i++ ) {
      cout << "Checking" << i << " for primality\n";
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks.....I got an idea on how to do it, but this helps!

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    I know what a prime number is, i know there is some way to test it using boolean. I was just freaking out, I thought it was due today but i still have a week on it, but ill figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  2. Need help creating prime number program
    By dauden6 in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2009, 02:50 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  5. Replies: 6
    Last Post: 11-04-2002, 05:11 PM