Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Help

C++ Help

Scheduled Pinned Locked Moved C / C++ / MFC
c++designhelplearning
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glowskull03
    wrote on last edited by
    #1

    i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena

    D N H 3 Replies Last reply
    0
    • G glowskull03

      i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena

      D Offline
      D Offline
      DaFrawg
      wrote on last edited by
      #2

      #include <iostream>

      bool IsPrime (unsigned int); // I don't know how you are going to check that, there are several ways to do that.

      int main(int argc, char* argv[])
      {
      unsigned int max;
      cout << "Give an integer number: "; cout.flush;
      cin >> max;
      cout << endl;

      for (unsigned int n = 1; n <= max; n++)
          if (IsPrime(n))
          {
              cout << n << ", ";
              cout.flush;
          }
      cout << endl;
      
      return 0;
      

      }

      D 1 Reply Last reply
      0
      • G glowskull03

        i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena

        N Offline
        N Offline
        namaskaaram
        wrote on last edited by
        #3

        herez one way of doing it.....;) #include #include void main(void) { int number; BOOL flag=0; clrscr(); cout<<"Enter the number : "; cin>>number; cout<<"The numbers are :"\n"; for(int j=1;j<=number;j++) { flag=0;//reset the flag..... for(int i=1;i

        1 Reply Last reply
        0
        • G glowskull03

          i'm trying to design a program that prompts the user for an integer adn then prints out all prime numbers up to that integer. i'm a beginner and i'm kind of stumped can anyone help j pena

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          Looks like homework to me, so I won't give you answers. I don't like the ones others have given, though they will work. (I even have a book someplace that tells how to check if a number is prime, but it fails on Newton primes - I can't recall exactly what they are called) What I would do, and what I suspect your professor wants: create a linked list of all known primes, up to the current number. This list starts empty. You start by checking the lowest unknown number against the list. If anything in the list divides your number it is not prime. If the number is prime place it in the list. Either way move to the next highest number. If this isn't enough, google will reveal several C programs that do the same thing. Don't bother handing them in though. Professors are fairly good at spotting those programs even after students hide the origions. Besides, I might get a job with you latter, and I'll want you to have done the work then.

          1 Reply Last reply
          0
          • D DaFrawg

            #include <iostream>

            bool IsPrime (unsigned int); // I don't know how you are going to check that, there are several ways to do that.

            int main(int argc, char* argv[])
            {
            unsigned int max;
            cout << "Give an integer number: "; cout.flush;
            cin >> max;
            cout << endl;

            for (unsigned int n = 1; n <= max; n++)
                if (IsPrime(n))
                {
                    cout << n << ", ";
                    cout.flush;
                }
            cout << endl;
            
            return 0;
            

            }

            D Offline
            D Offline
            dharani
            wrote on last edited by
            #5

            DaFrawg wrote: I don't know how you are going to check that, there are several ways to do that then why did u posted this ? redindian

            D 1 Reply Last reply
            0
            • D dharani

              DaFrawg wrote: I don't know how you are going to check that, there are several ways to do that then why did u posted this ? redindian

              D Offline
              D Offline
              DaFrawg
              wrote on last edited by
              #6

              I'm not making his homework. I just give him a sample skeleton, not the solution. Sure I know a way to find out if it is a prime or not, I've done that before. But if you can't think of one by yourself, you just have to take math classes.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups