• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prime Number Program  RSS feed

 
bisa rajesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every one . Here is my code why cannot i use if-else block instead of return in this program. please check it .

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (← that's a link) when you post code. I'll do it for you this time.

[Edit] Formatting your code is important too. Makes sure the braces line up.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use return; you can use break instead.
 
bisa rajesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can we use break in this program ?

Not only "break" other easy way to implent the program ?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a partial solution. See if you can complete it:

 
bisa rajesh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My, that code is poor style, with not enough indentation, multiple declarations, etc.

You do not need to go up to n/2 when checking whether a number is prime. You can do something different. But you can avoid break and if‑else:
for(i=2;prime&&i<n/2;i++)...

You can see how you can make that legible with a bit of space in the code.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bisa rajesh,

Knute already gave you suggestion to UseCodeTags (<- link) when you post your code.
I have added them for you once again, as well as corrected code formatting - it was horrible, took me couple of minutes to fix.
After I fixed that, I managed to notice that code indentation still not good enough, as you're missing curly braces around latest "if" and "else". Do not omit curly braces even if they contain only by one statement.

If someone is intended for you to help, you should help them by using code tags and making code correctly formatted and indented - so they could read it first and identify where your program logic flaws.

what classes do you import with this statement and what classes do you use?
 
samaira kapoor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added indentation and code tags (always use them) to your post, and doesn't it look a lot better
We usually discourage and often delete complete solutions, but I shall let yours stand because it confidently told me that 35 is prime, so let's have a look and see where the error is.

By the way: you aren't using anything from java.util, so you can remove the import declaration.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!