[Solved] Prime numbers from 10-200 using while loops only C++ [closed]


There are so many mistakes in your code. I don’t understand your logic, but you can do this:

#include <iostream>
using namespace std;

int main ()
{
    // checking number starting from 10
    int n = 10;
    bool isPrime;

    // as long as the number is less than or equal to 200
    while(n <= 200) {

        // assume the number is prime
        isPrime = true;

        // since prime number is the number that can only be divided by 1 and itself,
        // check if this number can be divided by any number other than 1 or itself
        for (int i = 2; i < n; i++) {
            // if this number can be divided by any number other than 1 or itself,
            if (n%i == 0) {
                // then this is not a prime number, no need to check anymore
                isPrime = false;
                break;
            }
        }

        if (isPrime == true) {
            cout << n << endl;
        }

        // check the next number
        n++;
    }
}

2

solved Prime numbers from 10-200 using while loops only C++ [closed]