Ask Question
17 July, 00:26

Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the function in a complete program that prompts for a number and indicates whether or not it is prime.

+3
Answers (1)
  1. 17 July, 02:51
    0
    public static boolean isPrime (int number)

    {

    if (number<1)

    return false;

    else if (number = = 1||number = =2 ||number==3)

    {

    return true;

    }

    else

    {

    for (int i=2; i
    {

    if (number%i==0)

    return false;

    }

    return true;

    }

    }

    The complete program is given in the explanation section

    Explanation:

    import java. util. Scanner;

    public class PrimeNumber {

    public static boolean isPrime (int number)

    {

    if (number<1)

    return false;

    else if (number = = 1||number = =2 ||number==3)

    {

    return true;

    }

    else

    {

    for (int i=2; i
    {

    if (number%i==0)

    return false;

    }

    return true;

    }

    }

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter a number to check if it is Prime");

    int num = in. nextInt ();

    if (isPrime (num)) {

    System. out. println ("The number "+num+" is a prime number");

    }

    else {

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

    }

    }

    }

    Prime numbers are numbers divisible only by themselves and 1 Examples are: (2, 3, 5, 7, 11)
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function name isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers