Home > Enterprise >  A program that checks if the sum of the divisors are equal to the number doesn't work
A program that checks if the sum of the divisors are equal to the number doesn't work

Time:01-12

So I've tried to make a short program that would get an input n that would represent the number you want to put into it, then it checks if the sum of the divisors of this number is equal to it, then at the end it shows how many numbers that have the sum of the divisors equal to them, but for some reason, it doesn't work, it always shows 0

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int n, k, l, i, j, m;

    cin >> n;
    m = 0;
    l = 0;

    for (i = 0; i < n; i  ){
        cin >> k;
        m = 0;

        for (j = 1; j <= n/2; j  ) {
            if (k%j == 0) {
                m = m   j;
            }
        }
        if (k == m){
            l  ;
        }
    }
    cout << l;

    return 0;
}

CodePudding user response:

Using more meaningful names exposes the bug immediately:

int main()
{
    int tests, number, successes, test, candidate, sum;

    cin >> tests;
    sum = 0;
    successes = 0;

    for (test = 0; test < tests; test  ){
        cin >> number;
        sum = 0;

        for (candidate = 1; candidate <= tests/2; candidate  ) {
            if (number           
  •  Tags:  
  • c
  • Related