Like Tree4Likes
  • 1 Post By grumpy
  • 1 Post By jimblumberg
  • 1 Post By Elkvis
  • 1 Post By nonoob

question about prime number function

This is a discussion on question about prime number function within the C Programming forums, part of the General Programming Boards category; yo guys , i wrote this program and compiled in turboc, but it gets error"declaration syntax error" . Code: #include ...

  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,261
    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.
    qozmyox likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,711
    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
    qozmyox likes this.

  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
    2,370
    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.
    qozmyox likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

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

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, 09: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, 03:24 PM

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