Check Prime Number Using Function in Java
import java.util.*;
class primefuntion
{
static boolean isprime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
if(isprime(a))
System.out.println("Prime");
else
System.out.println("Not prime");
}
}
class primefuntion
{
static boolean isprime(int n)
{
int i,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of a");
a=sc.nextInt();
if(isprime(a))
System.out.println("Prime");
else
System.out.println("Not prime");
}
}
Comments
Post a Comment