mercredi 13 novembre 2019

Can someone tell me the logic of this code to find perfect number because I can't understand and i'm in beginning of learning c++

    #include <iostream>
    using namespace std;
    int main()

    {
        int n, i, j, sum;
        cout << "\n Jepe num : ";
        cin >> n;
        for (j = 1; j <= n; j++) // here we initialize j from 1 to ->>n 
        {
            sum = 0;
            for (i = 1; i <= j / 2; i++)   // from here I cant understand how to get the perfect number
                if (j % i == 0)  // what's the value of i?
                    sum += i;
            if (sum == j)  // here okay I understand that e perfect number is the sum of its positive 
            // divisors, excluding the number itself 

                cout << "\n Numri :" << j << " eshte perfect !" << endl;
            else
                cout << "\n Numri :" << j << "Nuk eshte perfect !" << endl;
        }


        cin.get();
    }

Please help me I have exam in for days about loops and I have to understand the logic. Can someone help me how can I improve my c++ skills. I have searched a bit and I have come to the conclusion that I have to learn a lot more.

Aucun commentaire:

Enregistrer un commentaire