While loop output issue

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.

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 (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 ] 
Last edited on
closed account (48T7M4Gy)
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 ;)
Last edited on
It worked. Thank you!
Topic archived. No new replies allowed.