Help with loops

Reply

Join Date: Oct 2004
Posts: 7
Reputation: jonoj is an unknown quantity at this point 
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Help with loops

 
0
  #1
Oct 6th, 2004
Hi everyone
I am new to java and am writing a program thats meant to have the following terminal output...
Enter a number: 29
29 is a prime number.
Enter a number: -7
Please enter a positive integer.
Enter a number: 45
45 is not a prime number.
Enter a number: 0
Exiting program...

I think there is meant to be four main structures to the program -
1. User enters number.
2. if number less than 0 then print "enter positive" and go back to 1
3. if number over 0 do following calculation - for(i=2; i <= number;
i--){ if number / i ==0 print not prime. else print prime
4. 1,2,3 keep repeating till 0 is pressed

I am not sure how to implement this into my program, linking it
together with the correct loops.

here is my current source code:

import java.io.*;

class PrimeChecker
{

public static void main(String[] args) throws IOException
{

BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

int number = 1;

System.out.print("Enter a positive integer number: ");
number = Integer.parseInt(stdin.readLine());

do
{

if(number <= 0)
{
System.out.println("please enter a positive integer.");
}

else if(number > 0)
{
for(int i = 2; i <= number; i--)
{

if((number % i) == 0)
{
System.out.println("" + number + " is not a prime number");
}

else
{
System.out.println("" + number + " is a prime number");
}
}
}
}
while(number != 0);

}
}

Thanks for any help
Jonathan
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help with loops

 
0
  #2
Oct 7th, 2004
Im not sure that a do-while loop is the best way to go about this, might be though. Right now I dont have time to fix it but I can a little later.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: jonoj is an unknown quantity at this point 
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Re: Help with loops

 
0
  #3
Oct 7th, 2004
ok thats cool
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Help with loops

 
0
  #4
Oct 7th, 2004
I believe this is something like what you are asking for..Let me know if it's not.
  1. import java.io.*;
  2.  
  3. class PrimeChecker
  4. {
  5.  
  6. public static void main(String[] args) throws IOException
  7. {
  8.  
  9. BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
  10. String inData;
  11. int number = 0;
  12. int count = 0;
  13. try {
  14. do {
  15. System.out.print("Enter a positive integer number: ");
  16. inData = stdin.readLine();
  17. number = Integer.parseInt(inData.trim());
  18.  
  19. if (number > 0 && (number % 2) != 0)
  20. {
  21. System.out.println("The number is prime\n");
  22. }
  23.  
  24. else if (number == 0)
  25. {
  26. System.out.println("Please enter a number larger than 0\n");
  27. }
  28. else
  29. System.out.println("The number is not prime\n");
  30. count++;
  31. }
  32.  
  33.  
  34. while (!inData.equals("//exit"));
  35. }
  36. catch (NumberFormatException e) {}
  37. System.out.println("Exiting. Please wait....");
  38.  
  39.  
  40. }
  41. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 7
Reputation: jonoj is an unknown quantity at this point 
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Re: Help with loops

 
0
  #5
Oct 12th, 2004
Hi server
Thanks for that.
Pretty similair to my final submitted assignment except I used a for loop for the prime claculation, the way you did the calculation is clever - if a number over 0 divided by 2 has a remiender 0 its not a prime else is a prime. I also had to compensate for 1... which is not a prime.
Thanks again
Jonathan

(btw how do I make the wrap text to code button work?)
hers my final code just for interest... :mrgreen:

/*
Assignment 2: Prime Checker
Written by Jonathan Fordyce - 75770
Started on 04/10/2004, completed on 10/10/2004
This program accepts a number input from the keyboard then if the number is over 0 it calculates wether or not the number is a prime, printing the answer, and prompting for user to enter another number. If the number is negative the program will keep repeating prompting for positive, if positive it will calculate and repeat prompt, and if input is zero the program will quit.
*/

import java.io.*;

class PrimeChecker
{

public static void main(String[] args) throws IOException
{

//creates keyboard input stream and defines variables
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

long number;
boolean prime;

//prints message and quit intructions
System.out.println("(Welcome to Prime Checker --- Press 0 to quit the program)");

//repeat the following while number input isn't 0 (quit)
do
{
//prompts user to enter integer number, and assigns variable as input
System.out.print("Enter a positive integer: ");
number = Long.parseLong(stdin.readLine());

//if input number is 0 quit program
if(number == 0)
{
System.out.println("Exiting Prime Checker...");
return;
}

//if input number is over 1 do following calculations
if(number > 1)
{
//assigns boolean prime as true
prime = true;

//creates divisor i which is numbers between 2 and number
for(long i = 2; i < number; i++)
{
//if the reminder of input number divided by i equals 0
if((number % i) == 0)
{
//print 'not a prime' and make boolean prime as false
System.out.println(number + " is not a prime number.");
prime = false;
break;
}
}
//if prime is true print 'is a prime'
if(prime)
{
System.out.println(number + " is a prime number.");
}
}

//else if input number is below 0 print 'enter positive'
else if(number < 0)
{
System.out.println("Please enter a positive integer!");
}

//else if input number is below 2 print 'not a prime'
else if(number < 2)
{
System.out.println(number + " is not a prime number.");
}
}
while(number != 0);
}
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC