csansoon

P6.17 P55307 P0003. Friend numbers

Nov 7th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int friendOf(int n) {
  5.     int nfriend=0;
  6.     for (int i=1; i<n; i++) if (n%i==0) nfriend = nfriend + i;
  7.     return nfriend;
  8. }
  9.  
  10.  
  11. bool areFriends(int n1, int n2) {
  12.     if (friendOf(n1)==n2 && friendOf(n2)==n1 && n1!=n2) return true;
  13.     else return false;
  14. }
  15.  
  16.  
  17.  
  18. int main() {
  19.     int n1,n2;
  20.     bool first=true;
  21.     while (cin>>n1) {
  22.         cin>>n2;
  23.         if (areFriends(n1,n2)) {
  24.             if (not first) cout<<",";
  25.             cout<<"("<<n1<<" "<<n2<<")";
  26.             first=false;
  27.         }
  28.     }
  29.     cout<<endl;
  30. }
Add Comment
Please, Sign In to add comment