C
C
CodeInside2015-12-16 22:51:42
C++ / C#
CodeInside, 2015-12-16 22:51:42

Why is the result of the function execution incorrect?

A number is called perfect if the sum of all its divisors is equal to itself. You need to write a search for such numbers in the entered interval.

int a, b;
cin >> a >> b;

  printf("Perfect numbers of range [%d;%d]:\n", a, b);
  for (int i = a; i <= b; i++)//перебор всех чисел в интервале
  {
    int sum = 0;//при каждом новом числе сумма обнуляется
    for (int j = 2; j < abs(i); j++)
      if (abs(i) % j == 0)//делитель - число, которое делит первичное число без остатка
      {
        sum += j;
      }
    if (sum == i)
      cout << i << '\t';
  }

The result is always 0. What ranges just did not take. What logical error did you make?
Yes, I know that it will not work for negative numbers, because the sum will always be positive. But even with positive numbers, the algorithm does not work correctly.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2015-12-16
@CodeInside

A perfect number (ancient Greek ἀριθμὸς τέλειος) is a natural number equal to the sum of all its own divisors (that is, all positive divisors other than the number itself ).

Forgot the unit. She is also a divider.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question