I am trying to find prime number from an given array. I could see 3 is a prime number in my array but it is printing as NOT prime number. But if I remove other numbers and keep only 3 in this array, 3 prints as prime number only. Could anyone suggest what is the problem going on here ?
You need to check where you are setting the boolean value back to true.
posted 3 years ago
Welcome to the Ranch
Please look at the contents page for this forum, where you will read this:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
By posting a complete solution, you are depriving the OP of the chance to learn from the problem. It is even worse if there is no explanation of the problem. Finding the problem for onesefl will teach far more.
Please don't be annoyed, but I have pulled rank and removed the complete solution.
Sharmistha Sarkar wrote:I am trying to find prime number from an given array....
And maybe that's where your problem is. Why not just try to find out if A (single) number is prime or not?
Then you can concentrate on the problem and forget about all that boilerplate code, eg:Do you see the difference? Now your isPrime() method only has ONE thing to do: work out whether n is prime or not.
And if IT works properly, then all the other code must work.
BTW, very good to see you've separated out the Executor from the Prime class itself. Well done.
However, in general you'll want to make Primepublic as well, so I'd suggest making them separate classes in the same package.