Prime in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hirunisha
    New Member
    • Oct 2006
    • 3

    #1

    Prime in C++

    Hi Im a beginner in C++. I tried to do these but couldnt.
    Pls hekp me to do these. thanks.

    1. Design and implement a class called PrimeChecker. An instance of this
    class will be able to check if a number between 2-10000 is prime. The class
    stores the consecutive primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
    41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 in an array and uses
    these to check if the given number is prime.
    The class has the following public methods:
    (a) Constructor
    (b) Destructor
    (c) IsPrime() which takes a parameter which is an integer in the range
    1-10000 and returns a boolean.

    2. Extend the class above so that if the number x (which may now be more
    than 10000) to check is greater than the square of the largest prime in
    the list of consecutive primes then the class adds new consecutive primes
    to its internal array (the largest prime it adds is just greater than the
    square-root of x.). Assume that you will never need to hold more
    than 1000 consecutive primes.
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Use modulus


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int num;
        cin >> num;
        // num % 2 computes the remainder when num is divided by 2
        if ( num % 2 == 0 )
        {
            cout << num << " is even ";
        }
    
        return 0;
    }

    Comment

    • hirunisha
      New Member
      • Oct 2006
      • 3

      #3
      HI, thanks for the reply.
      But this won't sort my problem, its relate to classes.

      Comment

      • hirunisha
        New Member
        • Oct 2006
        • 3

        #4
        I could write a porg to check whether a number is prime or not.

        #include<iostre am>
        #include <cmath>
        using namespace std;

        int main()
        {
        int n;
        cout<<" Enter a number to find if its a prime number "<<endl;
        cin>>n;

        bool a =true;

        for(int i=2;i<sqrt(n);i ++)
        {

        if(n%i==0)
        {
        a=false;
        break;
        }
        }


        if(a==false)
        cout<<n<<" is not a prime number"<<endl;
        else
        cout<<n<<" is a prime number"<<endl;

        }


        I dnt knw hw to develope this for the followings:


        1. Design and implement a class called PrimeChecker. An instance of this
        class will be able to check if a number between 2-10000 is prime. The class
        stores the consecutive primes 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37,
        41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101 in an array and uses
        these to check if the given number is prime.
        The class has the following public methods:
        (a) Constructor
        (b) Destructor
        (c) IsPrime() which takes a parameter which is an integer in the range
        1-10000 and returns a boolean.

        2. Extend the class above so that if the number x (which may now be more
        than 10000) to check is greater than the square of the largest prime in
        the list of consecutive primes then the class adds new consecutive primes
        to its internal array (the largest prime it adds is just greater than the
        square-root of x.). Assume that you will never need to hold more
        than 1000 consecutive primes.

        Comment

        Working...