Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Java beginner needing help with Random Prime Number Generator.

CampbellJCampbellJ Posts: 1Member
Here is my dilemma...

I am in my first programming course, and I am supposed to be developing a random prime number generator. Unfortunately I have hit a snag and need some assistance. Can anyone help me? My source code is below, my issue is when I run my set of numbers (2-20) only 2 is registered as prime and the rest go as not prime. Any suggestions?

import java.util.*;
public class PNG
{
public static void main(String[] args)
{
int num1, num2, count, i = 2, prChk = 0 , m = 0;
Scanner keyIn = new Scanner (System.in);


System.out.print("Enter First number : ");
num1 = keyIn.nextInt();

System.out.print("Enter Second number : ");
num2 = keyIn.nextInt();

count = num1;

while(count <= num2)
{
while(i < count)
{
prChk = count % i;
if(prChk == 0)
m = 0;
else if(prChk == 1)
m = 1;
i++;
}
if (m == 0)
System.out.println(count + ": prime");
else if(m ==1)
System.out.println(count + ": not prime");
count++;
}
}
}

Thanks again!


James Campbell
Sign In or Register to comment.