I am learning C# and I struggle with a task.
I try to write a program which asks for a range of numbers, and return all the prime numbers.
However I always get the error:
Not all code paths return a value
First I made a function to check if a given number is a prime number, and it seems to work, here is the code:
public bool IsPrime(int theNumber)
{
for(int i = 2; i <= (theNumber/2)+1; i++)
{
if(theNumber%i == 0)
{
return false;
}
}
return true;
Now I want to loop it in a for
loop, and return all the prime numbers in that range... But it doesn't work.
Seems like the problem is that I am missing a return
statement in case the if boolean is false. But obviously I don't want a return
if my first method isPrime(int theNumber)
returns a false.
This is the code I have:
public int AllPrimesInRange(int lowerEdge, int upperEdge)
{
for(int i = lowerEdge; i <= upperEdge; i++)
{
if (IsPrime(i))
{
return i;
}
}
I hope someone can help me.. Thank you in advance.