fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int count = 0;
  7. vector<int> factors;
  8.  
  9. int n; cin >> n;
  10.  
  11. for (int i = 1; i <= sqrt(n); i++){
  12. if(n % i == 0){
  13. if((n / i) == i){
  14. factors.push_back(i);
  15. count++;
  16. }
  17. else {
  18. factors.push_back(i);
  19. factors.push_back(n/i);
  20. count += 2;
  21. }
  22. }
  23. }
  24.  
  25. cout << "The "<< count <<" factors of " << n << " are: " << endl;
  26. for(int i : factors){
  27. cout << i << " ";
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5316KB
stdin
36
stdout
The 9 factors of 36 are: 
1 36 2 18 3 12 4 9 6