Prime Factorization

hello guys. I need help. I am making a program that will output all pime factors of an integer.
This is my code

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using namespace std; bool isPrime(int n) { if(n < 2) return false; for(int m = 2 ; m <= sqrt(n) ; m++) { if (n % m == 0) return false; } return true; } int main() { int x = 32, y, p; cout << x << " = "; for(y = 2; y <= x/2; y++){ p=x%y; if(isPrime(p) && p!=0) cout << p << " x "; } return 0; }``````

my output is this

 `32 = 2 x 2 x 2 x 5 x 2 x 2 x `

it should be like this

 `32 = 2 x 2 x 2 x 2 x 2`
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include bool isPrime(int); bool isPrime(int n) { if(n < 2) return false; for(int m = 2; m <= sqrt(n); m++) { if (n % m == 0) return false; } return true; } int main() { int x = 32; std::cout << x << " = "; if (isPrime(x)) std::cout << x << std::endl; else { for (int y = 1; y < x; y++) { if (isPrime(y) && x % y == 0) { x /= y; std::cout << y << " * "; y--; //counter-act y++ } if (x == 2) std::cout << 2 << std::endl; } } return 0; } }``````
Last edited on
thank you sir for the help.

 ``12345678910111213141516171819202122232425262728`` ``````#include using namespace std; int main() { long long int num; while(cin >> num){ if(num<=1) cout << num << " is unexpected input"; else cout << num << " = "; for (int i=2; i <= num; i++) { while(num % i == 0) { num /= i; cout << i; cout << " x "; } } cout << endl; } return 0; }``````

and my input is this

 ```345 -2 32 4294967295```

and the output is this

 ```345 345 = 3 x 5 x 23 x -2 -2 is unexpected input 32 32 = 2 x 2 x 2 x 2 x 2 x 4294967295 4294967295 = 3 x 5 x 17 x 257 x 65537 x```

my problem now is the extra 'x' at the end of each equation. How will I remove them?
 ``12345678910111213141516171819202122232425262728293031`` ``````#include int main() { long long int num; while(std::cin >> num) { if(num <= 1) { std::cout << num << " is unexpected input" << std::endl; continue; } std::cout << num << " = "; for (int i = 2; i <= num; i++) { while(num % i == 0) { num /= i; std::cout << i; if (num > 1) std::cout << " x "; else std::cout << std::endl; } } } return 0; }``````
Thank you very much sir for the help!!
no problem :]
Topic archived. No new replies allowed.