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.
#include <iostream>
usingnamespace 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