ToniDev

Finding and printing divisors of a number N

Sep 21st, 2023 (edited)
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | Source Code | 0 0
  1. int main() {
  2.     int n;
  3.  
  4.     cout << "n = ";
  5.     cin >> n;
  6.  
  7.     // Finding and printing divisors using sqrt function
  8.     for (int i = 1; i <= sqrt(n); i++) {
  9.         if (n % i == 0) {
  10.             cout << i << " ";
  11.             if (i != sqrt(n)) {
  12.                 cout << n / i << " ";
  13.             }
  14.         }
  15.     }
  16.    
  17.     // Finding and printing divisors using for loop
  18.     /*
  19.     cout << "\nMetoda lui Toni: " << endl;
  20.     for (int i = 1; i <= n; i++) {
  21.         if (n % i == 0) {
  22.             cout << i << " ";
  23.         }
  24.     }
  25.     */
  26. }
Advertisement
Add Comment
Please, Sign In to add comment