# Thread: prime numbers using return 0

1. ## prime numbers using return 0

ok i know there is a million example programs to find out if entered number is prime. I want to do it a specific way and understand it at the same time. i am a complete novice as i am just learning some basic C in my unix class.
ok that all said here is how i want to write the code

int num,div=2;
scanf("%d",&num)
/* from here i want to run a loop that divides num by div until either return 0 is given meaning an integer( this is where what i want to do differs - i'm checking for an integer output and if an integer is returned printf("not prime") otherwise loop until div=num(although don't include div=num obviously) and if it completes all loops e.g returns 1 all the way through then printf("prime").
*/
/*i really don't know C enough to understand the syntax of where to put the div++ and how to write - if return 0 printf("not prime") else div++ - then if it completes all loops for div=2;div<!=num printf("prime") -
i hope this makes sense any input at this point is a plus.
*/

2. Search the board - this is asked pretty regularly.

3. Welcome to the forum, Minty!

Sounds like you want to get an integer from the user, and then test it with a for loop, using % (mod) operator, with increasing int's, to see if it's prime. Having it call another function is great if you want a certain return value.

In either case, your test for prime can stop at the sqrt() (but test the sqrt() itself), of the number being tested. Naturally, you want to calculate that sqrt(number), just once, before the for loop is entered.

This is called the "test by division" method, but uses % (mod), since you're interested only in the remainder.

4. ok thanks for the response. i guess i'm wrong about how i can do this. what i was getting at by trying to do it different was NOT using the % mod. i thought i could test for it being an integer by using something along the lines of - if return 0 (since int main() is used). basically i thought since it is expecting an integer then return 0 would mean an integer was returned and if a integer was returned using the division method then that means no remainder (since float is not being used). in other words i wouldn't need to use % mod. so basically if it goes through all its divisions in the loop up to the number inputed and and is still return 1 then it is prime. am i wrong about this use of returen 0 and return 1. can they not be used to test the condition without remainder. basicall if its return 0 on any pass then its not prime, right?
actually i was thinking while loop:
---------------------------------------------------------------------------------------
#include <stdio.h>
int main()
{
int div=1,num;
printf("enter number");
scanf("%d",&num);
while div<num
{
div++
num/div
if return 0 then printf("not prime")
}
printf("prime")

--------------------------------------------------------------------------------------
if the logic is right here i still need help with syntax. for exampled are my{} in right places, and can i use if then with return 0 that way. and the way its structured does that mean it will pass to printf("prime") if whats done in loop is never return 0?

5. Originally Posted by minty33
ok thanks for the response. i guess i'm wrong about how i can do this. what i was getting at by trying to do it different was NOT using the % mod. i thought i could test for it being an integer by using something along the lines of - if return 0 (since int main() is used). basically i thought since it is expecting an integer then return 0 would mean an integer was returned and if a integer was returned using the division method then that means no remainder (since float is not being used). in other words i wouldn't need to use % mod. so basically if it goes through all its divisions in the loop up to the number inputed and and is still return 1 then it is prime. am i wrong about this use of returen 0 and return 1. can they not be used to test the condition without remainder. basicall if its return 0 on any pass then its not prime, right?
I *was* thinking of return 0 or 1, being the return from another function within the program, instead of a return from main() itself.

What you've described is quite possible, but impractical. Your code as written now, gets input from the user (not *argv[]), so you would have to start your program, then enter the number to be checked, and then you'd get your answer AS THE PROGRAM ENDED.

So to check other numbers for prime, you'd have to restart your program for each number.

If you are repeatedly dividing, and acting upon the remainder of the division, aren't you using mod, in effect, by re-creating it?

Anyway, I'd keep your program running, and use the return from a test function you write, to determine whether a number is prime or not. It's easy enough to have the answer printed out, and let the program end after only one number, instead of using the return int from main().

num/div won't tell you if there was a remainder or not.

[*code]
Remove the * and paste your code between the code tags, or the forum software will smash it all over to the far left, and make it almost unreadable. The advanced editor also has an icon to get a pair of code tags - just highlight your code, and click on the code tags.
[*/code]

6. ok thank you for the advices. i think i understand what your saying. i'll mark this solved.