Computer Science, asked by Prisilla, 6 months ago

Create a function which accepts an integer as parameter and return true if it is a prime number otherwise return false. In the main () method input an integer and using above method check whether it is a prime number or not​

Answers

Answered by anindyaadhikari13
8

\star\:\:\:\sf\large\underline\blue{Question:-}

  • Create a function which accepts an integer as parameter and return true if it is a prime number otherwise returns false. In the main() method input an integer and using above method check whether it is a prime number or not.

\star\:\:\:\sf\large\underline\blue{Source\:Code:-}

class Program

{

static boolean isPrime(int n)

{

int c=0;

for(int i=1;i<=n;i++)

{

if(n%i==0)

c++;

}

return (c==2);

}

public static void main(String s[])

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n=sc.nextInt();

if(isPrime(n))

System.out.println("Number is prime. ");

else

System.out.println("Number is not prime. ");

}

}

Similar questions