User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 403,411 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,709 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2444 | Replies: 4
Reply
Join Date: Oct 2004
Location: Canberra, Australia
Posts: 5
Reputation: jonoj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Help Help with loops

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Help with loops

  #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  
Join Date: Oct 2004
Location: Canberra, Australia
Posts: 5
Reputation: jonoj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Re: Help with loops

  #3  
Oct 7th, 2004
ok thats cool
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Help with loops

  #4  
Oct 7th, 2004
I believe this is something like what you are asking for..Let me know if it's not.
import java.io.*;

class PrimeChecker
{

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

   BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
   String inData;
   int number = 0; 
   int count = 0;
  try {
   do {
       System.out.print("Enter a positive integer number: ");
       inData = stdin.readLine();
       number = Integer.parseInt(inData.trim());
     
       if (number > 0 && (number % 2) != 0)
       {
		System.out.println("The number is prime\n");
       }

       else if (number == 0)
       {
	System.out.println("Please enter a number larger than 0\n");
       }
	else 
 	 System.out.println("The number is not prime\n");
	count++;
    }
  

    while (!inData.equals("//exit"));
  }
  catch (NumberFormatException e) {}
	System.out.println("Exiting. Please wait....");
  
  
 }
}
 
Reply With Quote  
Join Date: Oct 2004
Location: Canberra, Australia
Posts: 5
Reputation: jonoj is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonoj's Avatar
jonoj jonoj is offline Offline
Newbie Poster

Solution Re: Help with loops

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 10:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC