Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org I have to write a function that returns the nth prime number in C. But getting error!
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 11-07-2014, 01:09 AM #1 pipideni LQ Newbie   Registered: Nov 2014 Posts: 3 Rep: I have to write a function that returns the nth prime number in C. But getting error! I might have mistakes in my code, any help would be welcomed. But compiling error is expected declaration or statement at end of input!!! Cant find out why. I would be thankful so much for any help. #include int isPrime(int number); int prime(int n); int main (void) { int n; printf("Enter the ordinal number of prime number you want to print out.\n", n); scanf("%d", &n); int result; result=prime(n); printf("(%d)PRIME is: %d \n", n, result); return 0; } int isPrime(int number) { int counter=2; if (number <= 1) return 0; // zero and one are not prime for (counter=2; counter<=number/number-1; counter++) { if (number % counter == 0) return 0; } return 1; int prime (int n) { int orderOfPrime=0; int currentPrimeNumber; int counter; for(counter=0; counter
 11-07-2014, 01:56 AM #2 pan64 LQ Guru   Registered: Mar 2012 Location: Hungary Distribution: debian/ubuntu/suse ... Posts: 9,214 Rep: please use [code]here comes your code[/code] to keep formatting. What kind of error did you get? Can you post it?
 11-07-2014, 02:06 AM #3 pipideni LQ Newbie   Registered: Nov 2014 Posts: 3 Original Poster Rep: I got the "Expected declaration or statement at the end of input" in fry croon isPrime
 11-07-2014, 02:08 AM #4 pan64 LQ Guru   Registered: Mar 2012 Location: Hungary Distribution: debian/ubuntu/suse ... Posts: 9,214 Rep: and again, please edit your original post and insert [code] and [/code]. Probably there is no closing } at the end of the function isPrime().
 11-07-2014, 02:10 AM #5 pipideni LQ Newbie   Registered: Nov 2014 Posts: 3 Original Poster Rep: Okay I will sorry. No I have checked that, there must be smth else.
11-07-2014, 02:13 AM   #6
astrogeek
Moderator

Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,270
Blog Entries: 1

Rep:
Quote:
 Originally Posted by pipideni Okay I will sorry. No I have checked that, there must be smth else.
Perhaps you should check it again...

 11-07-2014, 02:16 AM #7 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,496 Rep: As displayed, there is definitely a missing closing brace for isPrime ... so better check again or you have pasted incorrectly into the first post above.
11-07-2014, 06:21 AM   #8
johnsfine
LQ Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep:
What did you intend the following code to do?

Quote:
 Originally Posted by pipideni for (counter=2; counter<=number/number-1; counter++) {
number/number is 1.

 11-07-2014, 07:47 AM #9 c0d3d Member   Registered: Aug 2012 Posts: 74 Rep: I figure I should tell you that LinuxQuestions has its on Programming forum, and posting these kinds of questions there might get you better responses.
 11-07-2014, 07:58 AM #10 rtmistler Moderator   Registered: Mar 2011 Location: Sutton, MA. USA Distribution: MINT Debian, Angstrom, SUSE, Ubuntu Posts: 5,127 Blog Entries: 10 Rep: Firstly the function isPrime() does not have a closing bracket. That will get it to "compile". Next if you use -Wall as part of the compilation, you get the following warnings/errors: Code: ```gcc -o prime -Wall prime.c prime.c: In function ‘main’: prime.c:8:5: warning: too many arguments for format [-Wformat-extra-args] prime.c: In function ‘prime’: prime.c:37:12: warning: the address of ‘isPrime’ will always evaluate as ‘true’ [-Waddress] prime.c:32:9: warning: unused variable ‘orderOfPrime’ [-Wunused-variable] prime.c:35:5: warning: ‘currentPrimeNumber’ is used uninitialized in this function [-Wuninitialized]``` The warning for line 8 is because you have the argument n for your printf when you have no arguments in the format string such as %d. The warning for line 37 is because you've not "called" the function isPrime so the compiler interprets this as the address of the function isPrime for lack of any better choices. The correct thing to do here is to recode that line to be: Code: `if(isPrime(counter) == 1)` However that function appears to be ... weird. You passed a variable to it and you don't seem to use it. You don't initialize currentPrimeNumber, but you use it as a loop limit; so that can be and will be therefore arbitrary, probably some odd/very large indeterminate number. I guess instead of using counter for the isPrime() call your intentions would be to use the function argument of n. The warning for line 32 is because you never used a declared local variable. The warning for line 35 is exactly what I've covered above. A suggestion is that you fix the closing bracket issue for the isPrime() function, fix these warnings, and look more closely at your code to determine if it's really doing what you intend it to do. Another concern by the way is your for loop here: Code: ` for (counter=2; counter<=number/number-1; counter++) {` C language precedence states that * and / are ABOVE + and - in precedence. That is to say that "number/number" will ALWAYS be evaluated first. Therefore the result of that will ALWAYS be ONE leaving the next evaluation to be "number/number-1" --> "1-1" --> "0". Ergo, the loop will NEVER execute. Last edited by rtmistler; 11-07-2014 at 07:59 AM.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post dpattnaik Linux - Newbie 0 06-22-2009 02:39 AM 1subject Programming 1 06-17-2009 11:17 AM ta0kira Programming 2 01-01-2009 01:07 AM sachitha Programming 2 10-20-2006 02:47 PM sudheernair Programming 2 05-07-2004 11:14 AM

All times are GMT -5. The time now is 05:56 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -