prime numbers using return 0

This is a discussion on prime numbers using return 0 within the C Programming forums, part of the General Programming Boards category; ok i know there is a million example programs to find out if entered number is prime. I want to ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    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.
    thanx in advanced
    */

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Search the board - this is asked pretty regularly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    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?
    Last edited by minty33; 10-13-2012 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by minty33 View Post
    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]
    Last edited by Adak; 10-13-2012 at 07:08 PM.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    ok thank you for the advices. i think i understand what your saying. i'll mark this solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2012, 03:07 AM
  2. Prime numbers
    By GolDRoger in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 06:54 AM
  3. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 11:10 AM
  4. Prime numbers
    By cubbies5 in forum C++ Programming
    Replies: 11
    Last Post: 03-30-2011, 10:19 AM
  5. prime numbers
    By Murdogg7 in forum C Programming
    Replies: 1
    Last Post: 09-25-2002, 04:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21