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) {       ...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
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...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the...
PP 8.2 Modify the program from PP 8.1 so that it works for numbers in the range between −25 and 25 THIS IS MY CODE! please edit this...Thank you import java.util.Scanner; public class Array { public static void main (String[] args) { Scanner scan = new Scanner (System.in); int[] integs = new int[51]; System.out.println("Enter integers 0-50 [enter -1 to quit]: "); int nums = scan.nextInt(); while (nums != -1 && nums>=0 && nums<=50) { integs[nums]++; nums = scan.nextInt(); } for...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
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; /** * 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 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...
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...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...