Prime Factors of a Number

I have to create a program where I need to find the prime factors of a number and express it using exponents.

For example, for the number 1368 should output:

1368 = 2^3 * 3^2 * 19^1

This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void primeFact (int n){
   int i;
   int score = 0;
   for (int i = 2; i <= n; i++){
     if (isPrime (i) == true){
       while (n%i == 0){
         n=n/i;
         score++;
    }
    cout << i << '^' << score << " * " << endl;
    }
    }
}



I know it is incorrect, but I don't know how to fix it. Any help will be appreciated.
you have not declared any 'isPrime' function. Tell me what is it? and does this function do?
You should set score to 0 for every loop.


1
2
3
4
5
6
7
8
9
10
11
bool isPrime (int n) {
	if (n < 2){
		return false;
	}
	for (int i=2; i<=sqrt(n); i++){
			
		if ((n%i) == 0) {
			return false;
		}
		}
		return true;
Topic archived. No new replies allowed.