I'm having an issue to where the program will only say the first two numbers are prime. If somebody could point me in the right direction I would appreciate it.
#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 (factor == 0)
{
cout << " It is prime!";
}
cout << "\n";
n++;
}
return 0;
}
Here's the output when 2 and 10 are put in.
FACTORS AND PRIMES PROGRAM
This program will find factors and primes.
Enter a starting number: 2
Enter an ending number: 10
Finding factors and primes between 2 and 10
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: []
The factors of 6 are: [2 3 ]
The factors of 7 are: []
The factors of 8 are: [2 4 ]
The factors of 9 are: [3 ]
The factors of 10 are: [2 5 ]
You need to reset factor to zero ( factor = 0; ) after each cycle is complete for a particular number being tested. line 46/47 area might be a good place ;)