Keep count of times an If statement is used

I need to figure out how to keep track of how many prime numbers there were in the code written below. I'm kind of lost on how to do this so any help would be appreciated.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main()
{
	//Heading
	cout << "FACTORS AND PRIMES PROGRAM" <<endl;
    	cout << "This program will find factors and primes. \n" <<endl;

	//Request inputs
    	int n1;
    	int n2;
	int factor = 0;
	cout << "Enter a starting number: ";
	cin >> n1;
	cout << "Enter a ending number: ";
	cin >> n2;
	cout << "\n";
    	cout << "Finding factors and primes between " << n1 
             << " and " << n2 << endl;
	
	//Outer Loop
    	int n;
    	n = n1;
	while (n <= n2)
	{
	   cout <<    "The factors of " << n << " are: [";
	   
	   //Inner Loop
    	   for(int i = 2; i < n ; i++)
	   {
	   
	        //Factor Output
		if(n % i == 0)
		{
		cout << i << " ";
		factor++;
		}
       	   }
    	        cout << "]";

		//Prime Output
    		if ((n % 2 != 0) || (n / 2 ==1))
		{
	        cout << " It is prime!";
	        }

                cout << "\n";
	        n++;
	   }

    
    return 0;

}


Example: If the inputs were 2 and 10, I would need the underlined part in the output below.


FACTORS AND PRIMES PROGRAM
This program will find factors and primes.

Enter a starting number: 2
Enter an ending number: 20
Finding factors and primes between 2 and 20

The factors of 2 are: [] It is prime!
The factors of 3 are: [] It is prime!
The factors of 4 are: [2 ] 
The factors of 5 are: [] It is prime!
The factors of 6 are: [2 3 ] 
The factors of 7 are: [] It is prime!
The factors of 8 are: [2 4 ] 
The factors of 9 are: [3 ] 
The factors of 10 are: [2 5 ] 
 
There were 4 primes
Last edited on
Why don't you do the same thing as what you did for factors? Create a variable and increment it after every prime found.
That worked. Thank you! However, I just realized I have an issue with my if statement that determines if a number is prime.
Topic archived. No new replies allowed.