Hello i am new to java and o have to write a program that determines the number of prime numbers less than N which is given by the user, but i have no clue where to start

I would start with the number 1, then test if it is prime, then go to 2, and so on until N. To test if it is prime, you might want to just compare dividing it to every number below it, to see if it divides evenly.

Of course, there are faster ways to do it, but I'll leave those enhancements to you.

To find all the prime numbers less than N...

Well, '1' isn't a prime number. '2' is the first prime number. So maybe we could write a loop that counts from '2' up to the largest number that is just less than 'N'.

Now that we have a number, let's call it 'i' (with a value between '2' and 'N-1'), that might be prime, how can we determine if it's prime or not? Well, we could loop through all the numbers between '2' and 'i-1' to determine if dividing 'i' by that number produces a remainder. The '%' operator would probably be useful.

I'll leave the "counting" part to you.

The result would not be very efficient. But it would probably work.


Often the best way to figure out how to write a computer program is to figure out how you would do it by hand. You have to know how to do something before you can hope to teach the computer how to do it.

Think about this:
A prime number has ONLY two factors and those are 1 and the number itself.
For eg:
Factors of 2 are: 1, 2
Factors of 3 are: 1, 3
Factors of 7 are: 1, 7 and so on.....
That should get you started ;)

to find the no of prime nos less than N.
you have to find all the prime nos in the range of N.
start a loop from 1 to N
and check each and every no till N whether a no is prime or not.
if so take a counter and incement it everytime when u find a prime.

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.