- Your function has the wrong name (
is_primei
instead ofis_prime
). - You are always passing 0 to the function (
pr
specifically) instead of the input valuenum
.
Logic error for prime numbers
Question
I have attempted to rewrite a program I had for finding whether a number was prime or not, into a function. Currently I just output 1 or 2 just for testing purposes.
The problem is that p = 1 no matter what number is input.
Thanks to anyone with some insight.
# include <stdio.h>
int is_prime(int num);
int main()
{
int num, pr=0;
printf("Enter a positive number: ");
scanf("%d", &num);
printf("prime is %d ",is_prime(pr));
return 0;
}
int is_prime(int n )
{
int p, i, count=0;
for (i=2; i<=n/2; i++)
{
if(n%i==0)
{
count++;
break;
}
}
if (count==0 && n!=1)
p=1;
else
p=2;
return p;
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow