Here is my problem. I want the code to check to see if for one value of y that there is no remainder then display the message prime. Right now it is displaying the messages for every value of y.
Here is my code:
for (y = 2; y <= x-1; y ++) {
if (x % y == 0){
NSLog(@"Not prime");
}
else if(x % y != 0) {
NSLog(@"Is prime");
}
}
return 0;
}
}
You need to move the “is prime” outside of the loop. Your method checks to see if it isn’t prime and then prints “is prime” every time it iterates through the loop. You ONLY need to print “Is Prime” if it never prints out “Not Prime”. Your If-Else prints one or the other every time you check a number. A simple BOOL (boolean) can make this possible.
Try something like this: