Error in Prime Numbers Program

Here is what I have:

#include <iostream>
#include <cmath>

using std::cout;
using std::endl;
using std::cin;

int main(){

int x;
int i;
int j;

cout << "Please enter an integer 'x' greater than 3: " << endl;
cin >> x;
if (x <= 3){
cout << "Please enter new value 'x' greater than 3: " << endl;
cin >> x;
}
for(int i=3; i<=x; i++){
for(j=2; j<i; j++){
if(i%j == 0)
break;
else if(i == j+1);
cout << i << endl;
}
}
return 0;



This is the output after compiling when I enter 10 as 'x':
3
5
5
5
7
7
7
7
7
9


Can anyone tell me what I need to fix?
have you googled before posting?

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
34
35
36
37
38
    # include <cmath> // This library enable the use of sqrt.
    # include <iostream>
    using namespace std;
    void primenum(long double); // Prototype...
    int c = 0;
    int main()
    {
    long double x = 0;
    cout<<"\n This program will generate all prime numbers up to the"
    <<"\n number you have entered below...\n";
    cout<<"\n Please enter a number: ";
    cin>> x;
    cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
    primenum(x); //function invocation...
    cout<<endl<<"\nThere are "<<c
    <<" prime numbers less than or equal to "<<x<<".\n\n";
    return 0;
    }
    // This function will determine the primenumbers up to num.
    void primenum(long double x)
    {
    bool prime = true; // Calculates the square-root of 'x'
    int number2;
    number2 =(int) floor (sqrt (x));
    for (int i = 1; i <= x; i++){
    for ( int j = 2; j <= number2; j++){
    if ( i!=j && i % j == 0 ){
    prime = false;
    break;
    }
    }
    if (prime){
    cout <<" "<<i<<" ";
    c += 1;
    }
    prime = true;
    }
    }


By inoxmum

or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

    #include <iostream>
    int main()
    {
    int freq=0;
    for(int i=1;i<100;i++){
    int isZero=0;
    for (int j=1;j<i;j++)
    {
    if (i%j==0)
    isZero++;
    }
    if (isZero ==1)
    {
    freq++;
    }
    }
    std::cout << "The Frequecy of Prime Number is " << freq << std::endl;
    return 0;
    }


by ronan001
Last edited on
Topic archived. No new replies allowed.