Get total prime numbers

I have one program that show a prime numbers, like if i enter "9" as limit number the result is "2,3,5,7" (total 4 numbers) and i want to print the total numbers (i.e There are 4 prime numbers)

What should I do?

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
  #include <iostream>
  using namespace std;
  int main() {

    int x,a,b,cek,banyak;
    cout<<"Masukan batasan suku ke-n prima ";
    cin>>x;
    cout<<"================================================"<<endl;
    cout<<"Cetak bilangan prima dari batasan yang dimasukan"<<endl;

        for (a = 2; a<=x; a++){
        cek = 0;
            for (b = 2; b < a; b++)
            {
            	if (a % b == 0)
                {
                	cek++;
                }

            }

            if (cek == 0){
            	cout<<a<<" ";
            }

        }
        

    cout<<endl;
    cout<<"================================================"<<endl;
    cin.get();
    cin.get();
    return 0;
}
> cek++;
Here you managed to count the divisors.

> cout<<a<<" ";
So why not create another variable here, and increment it to count the primes.

You could count them!
1
2
3
4
5
6
7
8
9
10
    int counter = 0;    // declare and initialise
....
            if (cek == 0){
                counter++;              // increment count
            	cout<<a<<" ";
            }
.....
    cout<<endl;
    cout << "Found " << counter << " primes\n";    // Output the counter
    cout<<"================================================"<<endl;
Thanks to @lastchance and @salem c. My problem have been solver now
Topic archived. No new replies allowed.