Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Prime Number C++ using "While, For, Do...While" Loop?

I'm pretty new to programming. So please help me

Question:

Write a program using "while, for, and do...while" loop to determine is a prime number. A prime number is a number which is divisible by 1 or itself without leaving any remainder. For example: 4, 9. 15 and 16 are NOT a prime number while 2, 3, 5 and 7 are prime numbers.

That's all it says in the book. This is my post-lab activity and I need to send in 2 days. Please and thank you

4 Answers

Relevance
  • cja
    Lv 7
    1 decade ago
    Favorite Answer

    The Sieve of Eratosthenes is the classic way to solve this problem. And with the way I've done it below, you get all the kinds of loops you need.

    #include <iostream>

    #include <sstream>

    #include <deque>

    using namespace std;

    void drainSieve(deque<bool> &);

    int main(int argc, char *argv[]) {

        size_t n,x;

        string in;

        stringstream ss;

        do {

            cout << "Enter max value for prime search : ";

            getline(cin,in);

            ss.clear(); ss.str(in);

        } while ((!(ss >> n)) || (ss.good()));

        // Find primes

        deque<bool> sieve(n+1,true);

        drainSieve(sieve);

        // Prompt user to query for primes

        cout << "Enter numbers [0 .. " << n << "] to check." << endl;

        while (true) {

            cout << "> ";

            getline(cin,in);

            ss.clear(); ss.str(in);

            if ((ss >> x) && (x <= n) && !ss.good()) {

                cout << x << " is ";

                if (sieve[x] == false) cout << "not ";

                cout << "prime." << endl;

            }

        }

        return 0;

    }

    void drainSieve(deque<bool> &ps) {

        size_t N = ps.size();

        ps[0] = ps[1] = false;

        for (size_t i = 2; i*i < N; i++) {

            if (ps[i] == true) {

                for (size_t j = i+i; j < N; j += i) {

                    ps[j] = false;

                }

            }

        }

    }

    #if 0

    Sample run:

    Enter max value for prime search : 12000

    Enter numbers [0 .. 12000] to check.

    > 71

    71 is prime.

    > 10711

    10711 is prime.

    > 8

    8 is not prime.

    >

    #endif

  • ?
    Lv 4
    5 years ago

    oftentimes conversing, a for loop of here kind: for(statement1 ; statement2 ; statement3) { statement4; } might properly be rewritten as a on an identical time as loop of here kind: statement1; on an identical time as(statement2) { statement4; statement3; } in case you exchange your loop in this way, you will in all probability discover it works.

  • Anonymous
    7 years ago

    #include <iostream>

    using namespace std;

    int main()

    {

    long long int number=1,b;

    cout<<"please insert two numbers to find out wheather this number is prime or not:";

    cin >> number >>b;

    while (number <b)

    {

    for (long long int i=2;i<number;i++)

    {

    if (number%i==0)

    break;

    if (number==(i+1))

    cout << "number" << number << " is odd \n";

    }

    number++;

    }

    return 0;

    }

  • 7 years ago

    using do while?

Still have questions? Get your answers by asking now.