Prime Factors

I have an assignment that I just can't figure out:
Write a program that displays the prime factors of an integer entered by the user. If a number is a prime factor more than once, it should be printed more than once. The program output should look similar to:

Enter a number: 140
Prime factors: 2,2,5,7

Use the following algorithm:
Initialize a counter at 2
So long as the counter is less than or equal to the number
if the counter divides the number evenly
display the counter
divide the number by the counter to get a new number
else add one to the number



1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream.h>
int main() 
{ 
	int number; 
	int x; 
	cout<<"Enter a number: "; 
	cin>>number; 
	for (x=2; x<=number; x++) 
		if (x/number==2) 
			cout<<x<<endl; 
		else x++; 
		return(0); 
}
Last edited on
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
#include <iostream>

using namespace std;

int main()
{
	char choice;
	int number = 0;
	do {
	cout << "Enter a number and I will give you the prime factors: "; cin >> number;
		for (int a=3; a < number; a++)
	{
        bool prime=true;
        for (int b=2; b*b<=a; b++)
         {
         if (a % b == 0) 
          {
          prime=false;
          break;    
          }
        }   
        if(prime) cout << a << " ";
    }
		cout << "\nRun this program again? (Y or N): " << endl; cin >> choice;
	} while (choice == 'y' || choice == 'Y');

	return 0;
}
Topic archived. No new replies allowed.