Advertisement
MeehoweCK

Untitled

Dec 10th, 2020
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int prints_dividers(int n)
  6. {
  7.     if(n == 0)
  8.         return 0;
  9.     cout << 1 << '\t';
  10.     int counter = 1;
  11.     for(int i = 2; i < n; ++i)
  12.     {
  13.         if(n % i == 0)
  14.         {
  15.             cout << i << '\t';
  16.             ++counter;
  17.         }
  18.     }
  19.  
  20.     if(n > 1)
  21.     {
  22.         cout << n << endl;
  23.         ++counter;
  24.     }
  25.     else
  26.         cout << endl;
  27.     return counter;
  28. }
  29.  
  30. int main()
  31. {
  32.     cout << "Please enter a number: ";
  33.     int number;
  34.     cin >> number;
  35.  
  36.     while(cin.fail() || number < 0)
  37.     {
  38.         cout << "You entered the wrong number. Please enter a natural number again: ";
  39.         cin.clear();
  40.         cin.ignore(9999, '\n');
  41.         cin >> number;
  42.     }
  43.  
  44.     int dividers = prints_dividers(number);
  45.     cout << number << " has exactly " << dividers << " natural dividers.";
  46.     if(dividers == 2)
  47.         cout << " It is a prime number.\n";
  48.     else
  49.         cout << endl;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement