[Solved] Find whether a random number is Prime or not and Store in a character value


You made a couple of mistakes

#include <math.h>
#include <iostream>
using namespace std;
int main() {
    char ch1, ch2;
    for (int i=i; i<=20; i++) {
        int a = rand () % 20;
        cout << "The number is: " << a << endl;
        ch2 = 'y';
        //You only need to loop until its square root; after that is repetition
        for (int l=2; l<=sqrt(a); l++) {
            //You need to break if a divisor is found, else it is always going to say it's prime
            if (a%l==0){
                ch2 = 'n';
                break;
            }
        }

        cout << ch2 << endl;
        cout << "Enter 'y' if number is prime else 'n'" << endl;
        cin >> ch1;
        if (ch1 != ch2) {
            cout << "You lost" << endl;
            break;
        }
    }
}

I also did some cleanup of your code

0

solved Find whether a random number is Prime or not and Store in a character value