473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Prime factors

Iam having trouble getting started on this program and wanted to know if I could get help on writing a method and loops to get started. this is the program that I have to do:





Write a program that reads an integer number n > 1, and writes the input number’s prime factors. The following are a few input/output examples:

Input Output
12 2,2,3



15 3,5


28 2,2,7



5 5



Your program must contain one class, PrimeFactors. PrimeFactors contains the main method and a private static boolean method, isPrime. isPrime must accept one integer parameter and return true, if the input parameter is a prime number (a number is prime if it is only divisible by 1 and itself. For example 5 is a prime number but 6 is not a prime number.) Otherwise, isPrime must return false.

The main method must:

1. Display a banner describing the purpose of the program and how it can be used.
2. Prompt the user for an input number.
3. If the input value is less than or equal to 1, the user must be informed of the error in the input, and then be prompted for another input
4. If more than three consecutive erroneous input values are entered, the program must be terminated with an appropriate error message
5. If the input is larger than 1, then its prime factors must be displayed
6. After printing the prime factors, the program must ask whether or not the user wants to enter another input (Do you want to continue? (y/n))
7. The user must answer either yes, no, y or n using any combination of lower and upper case letters
8. if an incorrect answer is given, the user must be informed that the answer was not acceptable, and then be prompted for another answer.
9. If more than three consecutive erroneous answers are given, the program must be terminated with an appropriate error message.
10. If the answer is Y (or YES or any combination of lower and upper case letters,) the program must continue from step 2.
11. If the answer is No (or N or any combination of lower and upper case letters,) the program must terminate with a message informing that the program is terminated normally.
Oct 10 '07 #1
3 5061
r035198x
13,262 8TB
And what have you done so far?
Oct 10 '07 #2
I am having a problem listing the last interger for example when I put in 28 it suppose to say 2,2,7 but instead it says 2,2. My second problem is placing the method to make the program display attemps which is questions 6-11 on my original post.










package primefactors;
import java.util.Scanner;
/**
*
* @author rcarw001
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner in= new Scanner(System.in);
System.out.println("Enter an Interger");
int n=in.nextInt();
int p=2;
while (n>p)
if (n % p==0){
System.out.println(p);
n=n/p;
}
else
p=nextPrime(p);
}

public static int nextPrime(int number) {
int i= number + 1;
while (!isPrime(i))
{
i= i+1;
}

return i;
}

private static boolean isPrime(int number) {
int i= 2;
while (i < number){
if (number % i==0)
return false;


else
i=i+1;


}

return true;
}

}
Oct 21 '07 #3
JosAH
11,448 Expert 8TB
You should print the last number (after finishing the loop) as well.Do it by hand:
e.g. the number 28 can be divided by 2 so you print 2 and keep 28/2 == 14.
14 can be divided by so you print 2 and keep 14/2 == 7. Nothing divides 7 so
the loop ends and you have to print 7.

kind regards,

Jos
Oct 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
5
by: Rahul | last post by:
HI. Python , with its support of arbit precision integers, can be great for number theory. So i tried writing a program for testing whether a number is prime or not. But then found my function...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
7
by: Freyr | last post by:
Hello, I'm taking an independant course in C++, and one of my questions asks to use the following algorithm to deturmine the factors of any given number: -- Initialize a counter at 2 So Long...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.