So I was trying to get the prime number between two numbers, it works fine but it also prints out odd numbers
int prime(int num1, int num2)
{
int sum{0};
while (num1 <= num2)
{
for (int i = 1; i <= num1; i++)
{
if (num1 % i == 0) //remainder needs to be zero
{
if (num1 % 2 == 0) continue; //continue the loop if the number is even
if (i * num1 == num1 && num1 / i == num1)
{//make sure it only prints prime numbers
cout << num1 << endl;
sum += num1;
}
}
}
num1++;
}
return sum;
}
I tried to make a for loop that iterates between 1 and N and if the remainder is zero it also checks if the number is even.. if it is then it continues the loop. and then i checked for prime numbers.. if (i * num1 == num1 && num1 / i == num1) i expected it to print out only prime numbers and last the sum of them but it also counts odd numbers