| | |
one last tiny little detail
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 10
Reputation:
Solved Threads: 0
Hi,
I have written a program that accepts a positive integer as input and determines whether or not the number is prime, it all works accept when a number is prime it should display " x is a prime number " and continue to prompt for user input ( loop ) but it will only display " please enter a number"
Could you have a look at my code and see what I am missing or what I have done wrong please?
Thank you!
Here is the code:
import java.io.*;
public class PrimalityTest
{
public static void main(String[] args)throws IOException{
// create a keyboard input stream
BufferedReader stdin = new BufferedReader (new
InputStreamReader(System.in));
// variables to store input
int Input;
do
{
// prompt for user input
System.out.print("Please enter a number: ");
Input = Integer.parseInt(stdin.readLine());
// test for prime number
for (int i = 2; i < Input; i++)
if ((Input % i) == 0)
{
System.out.println(Input + " is not a prime number");
return;
}
else
System.out.println(Input + " is a prime number");
// if number is 0 then exit
if (Input == 0)
{
System.out.println("Exiting program...");
}
else
// if number is negative
if (Input <= -1)
{
System.out.println("Please enter a positive integer.");
}
}
while (Input !=0);
}
}
I have written a program that accepts a positive integer as input and determines whether or not the number is prime, it all works accept when a number is prime it should display " x is a prime number " and continue to prompt for user input ( loop ) but it will only display " please enter a number"
Could you have a look at my code and see what I am missing or what I have done wrong please?
Thank you!
Here is the code:
import java.io.*;
public class PrimalityTest
{
public static void main(String[] args)throws IOException{
// create a keyboard input stream
BufferedReader stdin = new BufferedReader (new
InputStreamReader(System.in));
// variables to store input
int Input;
do
{
// prompt for user input
System.out.print("Please enter a number: ");
Input = Integer.parseInt(stdin.readLine());
// test for prime number
for (int i = 2; i < Input; i++)
if ((Input % i) == 0)
{
System.out.println(Input + " is not a prime number");
return;
}
else
System.out.println(Input + " is a prime number");
// if number is 0 then exit
if (Input == 0)
{
System.out.println("Exiting program...");
}
else
// if number is negative
if (Input <= -1)
{
System.out.println("Please enter a positive integer.");
}
}
while (Input !=0);
}
}
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Here is the completed code I worked out. Note - I expanded to a long to enable you to enter large numbers.
Java Syntax (Toggle Plain Text)
import java.io.*; public class PrimalityTest { public static void main(String[] args) throws IOException { // create a keyboard input stream BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); // variables to store input // int Input; long Input; // Added boolean flag boolean isprime; do { // prompt for user input System.out.print("Please enter a number: "); // Input = Integer.parseInt(stdin.readLine()); Input = Long.parseLong(stdin.readLine()); isprime = true; // Set/reset flag // test for prime number for (long i = 2; i < Input; i++) { if ((Input % i) == 0) { System.out.println(Input + " is not a prime number"); System.out.println("It is divisable by: " + i); isprime = false; break; // Break out of loop } } // See if we found a divisor if(isprime){ System.out.println(Input + " is a prime number"); } // if number is 0 then exit if (Input == 0) { System.out.println("Exiting program..."); } else // if number is negative if (Input <= -1) { System.out.println("Please enter a positive integer."); } } while (Input != 0); } }
![]() |
Similar Threads
- Sorting without an array (C)
- Computer Suddenly Slow... (Viruses, Spyware and other Nasties)
- What ListBox properties do I set to... (VB.NET)
- A problem (Troubleshooting Dead Machines)
- Unneeded outgoing connections - Tiny Personal Firewall issue (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: To all the member
- Next Thread: Primality Test
Views: 1820 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
6 android api apple applet application arguments array arrays automation binary bluetooth bold byte c++ chat class classes client code component coordinates database datagram doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework html ide ideas image ingres input integer internet intersect ip j2me java javaexcel javaprojects jmf jni jpanel jtextarea julia linux list loop map method methods mobile netbeans newbie nextline number object oracle pong print problem program programming project recursion recursive scanner screen sell server set size sms socket sort sql string swing test threads time transfer tree user web websites windows





