recursive prime factors

im suppose to Write a program that will be able to get the prime factors of the number input by the user. Make sure your program implements a recursive algorithm. also we are going to have the user input a number, and the program will find all prime numbers from 2 to that number. For an example, write all prime numbers from 2 to 3000. The last number should be 2999 in this case.

Example: 2,3,5,7,11,13,17.. etc.
my problem is when it find not a prime number it display number in stead of just displaying my message "is not a prime number"

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
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cmath>
using namespace std;


void get_primeFactors(int n)
{
     int i;
     double total = n*(n);

     for (i = 2; i <= total; i++)
         while (n % i == 0) {   
             
		    cout << i << ", ";    
          get_primeFactors(n /= i); 
            return;              
	    }
}

void get_primeNumbers(int a)
{
	 int s = 2;       
	double b = (a);
    for (int i = s; i <= b; i++)
    {

    for (int j = 2; j <= i; j++)
     {
        if ((i%j==0)&&(i!=j))    //Condition for not prime
            {
			
                break;
            }

	   else  if (j==i)             //condition for Prime Numbers
           {
                 
			   get_primeFactors(i);
           }
    }
   }
}





int main() 
{
    int n = 0;
    int a = 0;
    cout << "Enter a number : ";
    cin >> n;
    get_primeFactors(n);
    cout << endl;
    cout<<"Enter a another nummber : "<<endl;
    cin>>a;
    get_primeNumbers(a);
    system("PAUSE");
    return 0;




  

}
1
2
3
4
5
bool is_prime(int number, int param = 2)
{ if(param == number && number != 2) return false;
 else if(param+1 <= number && number != 2 && number != 1 && number%param == 0) return is_prime(number, param+1);
 else return true;
}

Code not tested
Last edited on
Topic archived. No new replies allowed.