My code is written to find prime numbers from 2 to num. The code outputs every value of x, once if it is prime and more if it is not. I would like to know how to just output prime numbers. I am new to programming so please explain what your new bits of code does.

Code:
#include <iostream>
using namespace std;

int main()

{

    int num=23;

    for ( int x=1 ; x<num ; x++)
    {
        for ( int y = 2; y<=x ; y++)
        {
            if ( x % y == 0 )
            {
               cout<< x <<endl;
            }
        }
    }
}