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