Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Java beginner needing help with Random Prime Number Generator. Posted by CampbellJ on 10 Mar 2009 at 5:22 PM
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
Thread Tree



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.