Skip to main content

prime number...

2 replies [Last post]
fastergrace
Offline
Joined: 2006-02-17

I have to write a java program that takes a positive number entered by the user and displays whether or not it is prime. The assignment states that an even number is prime if it is 2, and an odd number is prime if it isn't divisible by any integer between 2 and the square root of the number. I really hadn't the faintest clue where to start, but I took a stab at it. Here is the program that I came up with...

Any help would be greatly appreciated as I haven't the faintest clue how to do this and the assignment is due tomorrow..

import java.io.*;

public class PrimeNum
{
public static void main(String[] args)
throws java.io.IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String s1;
int num, count;
double sqr, num2;

System.out.println("Please enter a positive integer:");
s1 = br.readLine();
num = Integer.parseInt(s1);

count = 2;
sqr = Math.sqrt(num);
num2 = 0;

for(count=2; count<=sqr; count++){

System.out.println("Please enter a positive integer:");
s1 = br.readLine();
num = Integer.parseInt(s1);

num2 = (num/2) % 10;
if (num2>0)
count++;

else
System.out.println(num + " is a prime number.");
break;

}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
brinkley
Offline
Joined: 2003-06-06

Your topic is not related to JavaHelp and should not have been entered into the JavaHelp forum. Unfortunately it isn't related to a desktop issue but this is where we're going to move incorrectly placed topics

zander
Offline
Joined: 2003-06-13

Brinkley: this message came in via the mailing list, right? So what is the change that any replies are going to be read by the original poster?
I suggest to just delete messages from a malinglist you feel are in-appropriate.

Btw; why don't you just moderate the mailing list in the first place?