Thread: question about prime number function

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    8

    question about prime number function

    yo guys , i wrote this program and compiled in turboc, but it gets error"declaration syntax error" .
    Code:
    #include <stdio.h>
    int main()
    int isprime(int n)
    {
    if(n<2)
    return 0;
    for(int i=2;i<=n/2;i++)
    {
    if((n%i)==0)
    return 0;
    return 1;
    }
    }
    whats the problem over here ? help me out thanks .

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    main() is supposed to be a function in its own right - for example, that might call isprime().

    Prefixing the definition of isprime() with "int main()" doesn't magically turned isprime() into the main function of the program.

    If you don't understand what I said, open any basic textbook on C, and look up how to implement functions. Don't rely on guesswork like you clearly have.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well it seems that even the horrible Turbo-C knows you shouldn't define a function inside another function. You need to move the isPrime() definition to before main, and it really wouldn't hurt if you came into this century by obtaining a more modern compiler.


    Jim

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    8
    well i do have devc and bcw , but they dont work properly on my pc . if you know anything then suggest me where i can code my c programs .(well i dont have enough space on my computer for visual c)


    thanks both jim and grumpy

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by qozmyox View Post
    well i dont have enough space on my computer for visual c
    visual C++ is not recommended for compiling C code, because it doesn't implement any recent version of the C standard. I'd recommend code::blocks or eclipse.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Logic is not right. Will return "is prime" before all divisors are checked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help creating a prime number function!
    By jewleeuhn in forum C Programming
    Replies: 3
    Last Post: 11-29-2011, 10:26 PM
  2. Prime Number Function
    By askinne2 in forum C Programming
    Replies: 12
    Last Post: 10-30-2010, 08:23 PM
  3. Prime Number Function
    By mrcg in forum C Programming
    Replies: 10
    Last Post: 10-21-2009, 12:33 AM
  4. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM