I have to write a program that asks the user to input a positive integer. It should than output a message indicating whether the number is a prime number.

Recommended Answers

All 2 Replies

..'in my own way I prefer to put a condition like this:

int input=5;
if(input%2!=0)
System.out.print(input+" is prime number!");
else
System.out.print(input+" is not prime number!");

And what if the user enters 24?
Then he should check for 2,3,4,5,8,12. The algorithm for checking prime numbers is a little bit more complicated.
It will require a for loop from 2 till the number divided by 2. Then in the for-loop you will have the if statement like the one posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.