I need help with is fixing the errors in my programs output. Below is what I\'ve
ID: 3925482 • Letter: I
Question
I need help with is fixing the errors in my programs output. Below is what I've written and attached is a photo of my error message. What specifically should i add or delete in my code to make it run? Thank youYou will ask the user to enter a positive integer and then print out whether or not the number is prime or not. Use a boolean variable named isPrime as a flag for whether or not the number is prime. Your program will use a while or do-while loop to do the following: 1. Use the following print statement to prompt for user input:?System.out.println(“Enter a positive integer or 0 to exit: “);? 2. Check if the number is in the valid range:?a. If 0, end the program?b. If negative, print out an error message stating:?System.out.println(“Please enter a positive integer.”);?Then use System.exit(1) to end the program.? 3. Use a for loop to:?a. Check the multiples of the number from 2 through number / 2, inclusive, incrementing by 1 as you check the multiples. Your for loop header should look like this:?for(int mult = 2; mult <= num/2; mult++)?b. If the num % mult yields a 0, then the number is not prime. Break out of the loop since you know it’s not prime.? 4. Use if-else statements for when isPrime is true or when isPrime is false.?a. If prime, print out the following statement: System.out.println(num + “ is prime.”);?b. If not prime, print out the following statement:System.out.println(num + “ is not prime.”);? ——————————————————————
program inputs are -1 0;
0 1998 11 -1;
import java.util.Scanner; public class Lab4a{ public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int userNum; boolean isPrime = false; System.out.println("Enter a positive integer or 0 to exit:"); userNum = scnr.nextInt(); while (userNum != 0){ System.out.println("Enter a positive integer or 0 to exit:"); if(userNum <= 0){ System.out.println("Please enter a positive integer."); System.exit(1); } else { for (int mult = 2; mult <= userNum/2; mult++) { if (userNum % mult == 0) break; else isPrime = true; } } if (isPrime = false) System.out.println(userNum + " is prime."); else System.out.println(userNum + " is not prime."); } } }
Explanation / Answer
// program for check whether number is prime or not if the number is positive
import java.util.Scanner;
public class Main{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
boolean isPrime=true;
int num;
System.out.println("Enter a positive integer or 0 to exit");
num=sc.nextInt();
// verify number is o or negative number
if(num<=0)
{
System.out.println("please enter a positive integer");
System.exit(1);
}
else
{
// verify number is prime or not
for(int i=2; i<=num/2; i++)
{
if(num % i == 0)
{
isPrime=false;
break;
}
}
}
if(isPrime==true)
{
System.out.println(num + " is prime");
}
else
{
System.out.println(num + " is not prime");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.