-2

I wrote a function to find the sum of divisors for a number n.

int divisor_sum(long n) {
    long sum = 0;
    for (int a=1, a<=n, a++) {
        if n % a == 0 {
            sum = sum + a;
        }
    }
    return sum;
}

Unfortunately, the program (which includes a main function skeleton) won't compile because it says that "'n' was not declared in this scope." I've tried declaring n as a long before and after the function definition statement to no avail. How do I fix this? Thanks

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458

1 Answers1

0

Like StoryTeller and O'Neil told you in the comments, you need to replace this

for (int a=1, a<=n, a++)

with

for (int a = 1; a <= n; a++)

and this

if n % a == 0

with

if (n % a == 0)
Sid S
  • 6,037
  • 2
  • 18
  • 24