# Thread: Recursive function that returns the sum of all prime numbers from 2-n.

1. ## Recursive function that returns the sum of all prime numbers from 2-n.

When i use 6 as my n, i get 0, which makes sense to me.;however, why do i get 6 for my final result? Why does it not do 0+5+...etc..?
Code:
```//source.c file
unsigned int sum_primes(unsigned int num)
{
//base case

if (num == 4 || num == 3)
{
return 3;
}
if (num%2==0||num%3==0)
{
return 0;
}
else
{
return num+sum_primes(num - 1);
}

}
//main

int main()
{
printf("%d\n", sum_primes(6));
}``` 2. Your code gives zero as a result. What are you talking about?

Also, that result doesn't make sense at all. The sum of all primes from 2 to 6 should be (2+3+5) = 10. 3. Sorry i meant to ask why i was getting zero as my answer. Yea i understand that it should be 10, but where did i go wrong? I understand why i get 0 for my very first num, because 10 is even; however, why does it not call itself again? 4. because 6 is even* 5. Well, it can't work because you immediately return 0 for all multiples of 2 or 3. In my opinion, this function is irredeemable. Recursion and prime numbers don't mix well... 6. oh i see. This is one of my hw assignments and it stated that recursion was mandatory. Imo i think a for loop would make this process much easier, but i can't do that. 7. Recursion may be mandatory, but is it the only method you're allowed to use? I'm asking because you could easily sneak an 'isPrime(num)' function in there. Like this:
Code:
```unsigned sum_primes(unsigned num)
{
unsigned result = 0;

if (isPrime(num)) {
result += num;
}
if (num >= 2) {
result += sum_primes(num - 1);
}

return result;
}```
There are of course many ways to make this function better, I leave this up to you. 8. Ah i see. Thank you very much for your time. Popular pages Recent additions #include, header.h, int, num, return 