Factors and Prime Numbers

I'm trying to write a program that will find all the factors and primes for a range of numbers. I have the inner loop working but I am having trouble writing the outer loop that will output the range of numbers instead of just finding the factors for one number. 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
    int n1 = 0;
    int n2;
	int factor = 0;
	cout << "Enter a starting number: ";
	cin >> n1;
	cout << "Enter a ending number: ";
	cin >> n2;
    cout << "Finding factors and primes between " << n1 
         << " and " << n2 << endl;
    cout << "The factors of " << n1 << " are: [";

    for(int i = 2; i < n1 ; i++)
    {
	if(n1 % i == 0)
	{
	cout << i << " ";
			factor++;
	}
    }
    cout << "]";
    if(factor == 0)
    {
    cout << " Number is prime!" << endl;
    }
 return 0;
}
Last edited on
So if the user inputs 2 for the starting number and 6 for the ending number the output would look like this:


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 ]
Last edited on
Would you just want to do something like this? Loop through the range the user provides?
1
2
3
4
for (int current = n1; current <= n2; ++current)
{
    //include loop you've already written, replacing every instance n1 with the variable current
}
Last edited on
hi:

The factors of both number?? or factors of each number between both numbers???

I going to help you with the primes:

int n1 = 0; I don't know why if you going to ask after a number n1. put just

int n1;

the loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(int i=n1 ; i<=n2 ; i++)
    {
        int factor = 0;

        for(int j=1 ; j<=i ; j++)
        {
            if(i%j == 0)
            {
                factor++;
            }
        }
        if(factor == 2)
        {
            cout<<i<<" ";
        }
    }
    cout<<"]"<<endl;

    cin.get();
    return 0;


This show you the prime numbers in the range.
Right now my code will output just the beginning number factors. So if six is the beginning number the output will be:


FACTORS AND PRIMES PROGRAM
Finding factors and primes between
This program will find factors and primes.
Enter a starting number: 6
Enter a ending number: 6
Finding factors and primes between 6 and 6
The factors of 6 are: [2 3 ]


I'm trying to write a loop that will print the factors from the beginning number to the ending number. So if the inputs are 2,6 it will output the following:


FACTORS AND PRIMES PROGRAM
Finding factors and primes between
This program will find factors and primes.
Enter a starting number: 2
Enter a ending number: 6
Finding factors and primes between 2 and 6
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 ]
So I think I have figured out the outer loop part. However, now I am running into an issue with the primes. For some reason it will not continue to print "It is prime!"

Here's how the code is written now:

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


using namespace std;
int main()
{
	//Heading
	cout << "FACTORS AND PRIMES PROGRAM" << endl;
    	cout << "Finding factors and primes between" << endl;
    	cout << "This program will find factors and primes." << 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 << "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;
}

If the input is 2, 6 it will output the following without stating 5 "is prime" :

FACTORS AND PRIMES PROGRAM
Finding factors and primes between
This program will find factors and primes.
Enter a starting number: 2
Enter a ending number: 6
Finding factors and primes between 2 and 6
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 ]
Looks like I just had to change the prime if statement to:

1
2
3
4
if ((n % 2 != 0) || (n / 2 ==1))
{
   cout << " It is prime!";
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/144496/
Topic archived. No new replies allowed.