QuestionAsk Questions?

how to add a comment indicating that the method will determine if a number is a prime number. In the next line add the method header for this method which will be called isPrime. This method returns the value of a type boolean and takes an argument of type int, the parameter will be named number. Add a left brace on next line. in the body of the method add a comment stating that 1 is not a valid prime number, on the next line add an if statement to test whether the value of the parameter number is equal to 1.

View Answers

October 22, 2010 at 10:58 AM

Hi Friend,

Try the following code:

import java.util.*; class CheckPrime{ static boolean isPrime(int number){ boolean isPrime = false; int i = (int) Math.ceil(Math.sqrt(number)); while (i > 1) { if((number != i) && (number % i == 0)) { isPrime = false; break; } else if (!isPrime) isPrime = true; --i; } return isPrime; } public static void main(String[] args){ int num = 5; if (isPrime(num)) { System.out.println("Number is Prime"); } else{ System.out.println("Number is not Prime"); } } }

Thanks

Related Tutorials/Questions & Answers:

Advertisements