Question

/*

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!");

}

}

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:**

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 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 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...

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 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 =...

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 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 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 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 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...

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 13 minutes ago

asked 20 minutes ago

asked 25 minutes ago

asked 36 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago