Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main()
- {
- cout << "Give me a natural number to tell you if it is a perfect number like 6 or 28." << endl;
- int n;
- cin >> n;
- vector <int> diairetes;
- // I put the diairetes in the vector, so I can add them then
- for (int i=1; i<n; i++){
- if(n%i == 0){
- diairetes.push_back(i);
- }
- }
- // Show the content of the vector
- cout << "DIAIRETES: " << endl;
- for(unsigned int i=0; i<diairetes.size(); i++){
- cout << diairetes[i] << endl;
- }
- cout << endl;
- // Check if n is a perfect number
- int sum = 0;
- for (unsigned int i=0; i<diairetes.size(); i++){
- sum += diairetes[i];
- }
- if (n == sum){
- cout << n << " is perfect." << endl;
- cout << n << " = ";
- for (unsigned int i=0; i<diairetes.size(); i++){
- cout << diairetes[i] << " + ";
- }
- cout << endl;
- }
- else{
- cout << n << " is not a perfect number." << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement