Sum of n Even numbers

Just trying to write a basic function that calculates the sum of n even numbers. Ex: n = 3 (2 + 4 + 6 = 12) -> Sum = 12. n = 4 (2 + 4 + 6 + 8 = 20) -> Sum = 20, etc. I also want to return the sum to the console. I'm currently getting an error on my n variable (expression preceding parentheses of apparent call must have (pointer-to-) function type)
Not too sure, what that exactly means. Any help would be greatly appreciated! Thanks in advance.

1
2
3
4
5
6
  int sumOfEvenNumbers(int n) {
    int sum = 0;
    sum += n (n + 1);

    return sum;   
}


1
2
3
4
int main(){
int sum = sumOfEvenNumbers(3);
cout << "Sum: " << sum;
}
Last edited on
What do you want line 3 to be doing?
If you're going for multiplication, you need to use *.
e.g. n * (n + 1)

Also, your function is called sumOfEvenNumbers, but you are calling it as sumOfEvenNumbers2.
Last edited on
@Ganado, yes I was trying to multiply that was my mistake and didn't notice the 2 as well, thanks a lot!
Topic archived. No new replies allowed.