I wrote a program for finding prime numbers, my problem is that the code only will run correctly, only if I take the square root of the tested number (on the marked line).
When I have int i = input
the code wont run.
I know that sqrt()
is helpful here but i thought it should work without as well.
#include<stdio.h>
#include<stdbool.h>
#include<math.h>
bool primeNumber(int input)
{
for(int i = sqrt(input); i > 1; i--) // <------- sqrt()
{
if(input % i == 0)
{
return false;
}
} return true;
}
int main()
{
int input;
printf("Pls type in a positive number: ");
scanf("%d",&input);
for(int i = input; i > 1; i--)
{
bool prime = primeNumber(i);
if(prime)
{
printf("%d is a prime number\n",i);
}
}
return 0;
}