C++ - program which verifies if a number is prime

Okay so my program has to verify if a number is prime, and that number is formed by the number of units of another numbers.
Example: for n=4 and numbers 237 23 453 11 the program will display 7331.
My problem is after I introduce n and the first number, I can't enter any other number.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n, x, nr, OK;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        while(x!=0)
        {
            nr=x%10;
        }
        OK=1;
        if(nr%2==0 or nr>3) {
            OK= (x==2); }
            else {
                    int d=3;
            while(d<=sqrt(x) && OK == 1)
            {
                if(x%2==0) {OK=0;}
                else {d+=2;}
            }
            if(OK==1) {cout<<"Numarul "<<nr<<"este prim.";}
            else {cout<<"Numarul "<<nr<<"nu este prim.";}
    }
    return 0;
}
}
line 13:

1
2
3
4
5
6
        cin >> x ; // for example x = 1;
        while( x != 0 ) // how does x to become 0 ??
        {
            nr = x % 10; // infinite loop because nr = x % 10 = 1 forever
                                // x is never decremented 
        }


line 19:

 
            OK = (x==2); // you are sure to want this? what would you like to do? 
Last edited on
Topic archived. No new replies allowed.