Advertisement
makispaiktis

Τέλειοι αριθμοί (6,28)

Aug 7th, 2018 (edited)
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     cout << "Give me a natural number to tell you if it is a perfect number like 6 or 28." << endl;
  10.     int n;
  11.     cin >> n;
  12.  
  13.     vector <int> diairetes;
  14.  
  15.     // I put the diairetes in the vector, so I can add them then
  16.     for (int i=1; i<n; i++){
  17.  
  18.         if(n%i == 0){
  19.  
  20.             diairetes.push_back(i);
  21.         }
  22.     }
  23.  
  24.     // Show the content of the vector
  25.     cout << "DIAIRETES: " << endl;
  26.  
  27.     for(unsigned int i=0; i<diairetes.size(); i++){
  28.  
  29.         cout << diairetes[i] << endl;
  30.     }
  31.  
  32.     cout << endl;
  33.  
  34.     // Check if n is a perfect number
  35.     int sum = 0;
  36.  
  37.     for (unsigned int i=0; i<diairetes.size(); i++){
  38.  
  39.         sum += diairetes[i];
  40.     }
  41.  
  42.     if (n == sum){
  43.  
  44.         cout << n << " is perfect." << endl;
  45.         cout << n << " = ";
  46.  
  47.         for (unsigned int i=0; i<diairetes.size(); i++){
  48.  
  49.             cout << diairetes[i] << " + ";
  50.         }
  51.  
  52.         cout << endl;
  53.     }
  54.  
  55.     else{
  56.  
  57.         cout << n << " is not a perfect number." << endl;
  58.     }
  59.  
  60.     return 0;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement