Declare a method to determine whether an integer is a primenumber Use the follow
ID: 3618684 • Letter: D
Question
Declare a method to determine whether an integer is a primenumber
Use the following method declarations: public static BooleanisPrime (int num)
An integer greater than 1 is a prime number if its onlydivisor is 1 or itself. For example, isPrime (11) returnstrue, and isPrime (9) returns false.
Us the isPrime method to find the first thousand prime numbersand display every ten prime numbers in a row, as follows:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 61 73 79 8389 97 … …
Declare a method to determine whether an integer is a primenumber
Use the following method declarations: public static BooleanisPrime (int num)
Explanation / Answer
x.Hlor="red">please rate - thanks I moved some things around so that it will continue after the numbers to 1000 are shown //imports import javax.swing.*; //Main class starts public class Main { // Main function starts public static void main(String[] args){ //Declare variable String numStr; //do starts do{ //prompts on the screen to user Enter a number between 1 and 1000 (0 to EXIT) numStr = JOptionPane.showInputDialog(null, "Enter a number betwen 1 and 1000 {-1 to SHOW PRIME NOS. BETWEEN 1 and 1000 / 0 to EXIT)"); // if user enters a blank than it will again ask to enter a number if(numStr==null || numStr.trim().equals(' ')){ continue; } //If user enters 0 exits the program if("0".equals(numStr)){ break; } //Parse the input number to integer type and assign it to a local variable int input = Integer.parseInt(numStr); //If input is greater than 0 it will check for prime or not prime if(input>0){ //If the number is prime, it will display on the screen as number is prime if(isPrime(input)){ JOptionPane.showMessageDialog(null, input + " is prime"); } else{ JOptionPane.showMessageDialog(null, input + " is not a prime"); } } //If the number is not prime, it will display on the screen as number is not prime else if (input == -1){ //Declare and initialize variables String sOut = " "; int iCounter = 0; //Calculates and displays the prime numbers between 1 and 1000 in console for(int iCount = 1 ; iCount < 1000 ; iCount ++){ boolean iPrime = isPrime(iCount); if(iPrime == true){ //If the number is prime add to the string sOut = sOut + " " + iCount; //Increment iCounter and when it reaches a multiple of 10 gives a line break iCounter++; if(iCounter%10 == 0){ sOut = sOut + " "; } } } numStr="---------------------------------------------- " + "Prime numbers from 1 to 1000 " + "---------------------------------------------- " + sOut + " ---------------------------------------------- "; JOptionPane.showMessageDialog(null, numStr); } //If the input is wrong it will prompt to enter again else{ JOptionPane.showMessageDialog(null, "Enter number > 0"); } }while(!("0".equals(numStr))); // While user chooses not to quit the prompt will continue while(true); } //Function isPrime to check if a number is prime or not public static boolean isPrime (int iNum) { //Declare variables and initialize it boolean bFlag = true; int iCount, jCount, iRemainder; //Two for loops checks if the number is prime or not for(iCount = 1 ; iCountRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.