Question

/* This program should check if the given integer number is prime. Reminder, an integer number...

/*

This program should check if the given integer number is prime.

Reminder, an integer number greater than 1 is prime if

it divisible only by itself and by 1.

In other words a prime number divided by any other natural number

(besides 1 and itself) will have a non-zero remainder.


Your task:

Write a method called checkPrime(n) that will take

an integer greater than 1 as an input, and return true

if that integer is prime; otherwise, it should return false.

Your main() would prompt the user to enter an integer

greater than 1 and then call checkPrime() on that number.

The main() would then print a statement whether the

number was prime or not.

For example, the program output can look like this:

Please enter an integer greater than 1: 2473 <--(user input)

2473 is a PRIME!

or like this:

Please enter an integer greater than 1: 6981

6981 is a NOT a prime!

*/

import java.util.Scanner;

public class CheckPrimes {

//Returns true if n is prime

public static boolean checkPrime(int n){

/*

The strategy is pretty simple:

In a loop, try dividing number n

by every integer i starting from 2

and finishing with n/2

If a remainder from that division

is 0, that means that n is divisible by i

and n is not a prime (return false then).

  

If none of the divisions give you remainder 0,

then n is prime and you should return true.

*/

return true;

}

public static void main(String[] args) {

//No need to change the main()

Scanner scan = new Scanner(System.in);

System.out.print("Please enter an integer greater than 1: ");

int num = scan.nextInt();

System.out.println();

if (checkPrime(num))

System.out.println(num + " is a PRIME!");

else

System.out.println(num + " is NOT a prime!");

  

}

}

 

Homework Answers

Answer #1

Program:

import java.util.Scanner;

public class CheckPrimes {
    
    public static boolean checkPrime(int n) {
        for(int i = 2; i <= n/2; ++i)
        {
            // condition for to check for nonprime number
            if(n % i == 0)
            {
                return false;
            }
        }
        return true;
    }
    
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter an integer greater than 1: ");
        int num = scan.nextInt(); // Taking input from user
        System.out.println();
        // Calling function and printing output
        if (checkPrime(num))
            System.out.println(num + " is a PRIME!");
        else
            System.out.println(num + " is NOT a prime!");
    }
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
A prime number (or a prime) is an integer greater than 1 that is not a...
A prime number (or a prime) is an integer greater than 1 that is not a product of two smaller integer. Created a program on visual studio named PrimeNumberTest that does the following: 1) prompt the user for input of an integer 2) test if the integer is a prime number 3) display the test result  
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
A prime number is an integer greater than 1 that is evenly divisible by only 1...
A prime number is an integer greater than 1 that is evenly divisible by only 1 and itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not. Create a PrimeNumber application that prompts the user for a number and then displays a message indicating whether the number is prime or not. Hint: The % operator can be used to determine if one number is evenly divisible by another. ( Java programing...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...