Answer to Question #47432 in Java | JSP | JSF for Yanelle

Question #47432
How to create a program that will accept five numbers and determine the prime numbers only using netbeans and JOptionPane
Expert's answer
packagejavaapplication1;
importjava.awt.Component;
importjavax.swing.JOptionPane;

publicclass JavaApplication1 {
private static Component frame;

/**
* @param args the command line arguments
*/
public static voidmain(String[] args) {
userPrimes();
}

public static voiduserPrimes(){
int[]tempPrimes = new int[49];
intprimesFound = 0;
//InputAmount of numbers to be analysed
int[]initial = newint[Integer.parseInt(JOptionPane.showInputDialog("Enter Amount of numbers to be checked"))-1];
//InputValues
for(int i = 0; i<initial.length; i++){
initial =Integer.parseInt(JOptionPane.showInputDialog("Enternumber at position:" + (i+1)));
if(initial[i] > 49){
initial[i] = Integer.parseInt(JOptionPane.showInputDialog("Numbers cannot be greater than 49, Try again:"));
}
}

for(int i = 0; i<initial.length; i++){
if(isPrime(initial[i]) == true){
tempPrimes[i] = initial[i];
primesFound++;
}
}

int[]finalPrimes = new int[primesFound];
String str = "Primenumbers: ";
for (int i=0;i<finalPrimes.length;i++){
finalPrimes[i] = tempPrimes[i];
str+=finalPrimes[i] + " ";
}
JOptionPane.showMessageDialog(frame,str);

}

//checkswhether an int is prime or not.
staticboolean isPrime(int n) {
for(int j = 2; j < n; j++) {
if(n% j == 0) {
returnfalse;
}
}
return true;
}
}
[i]


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS
paypal