An integer is said to be prime if it is divisible only by two distinct factors 1 and itself. For example 2,3,5 and 7 are prime, but 4,6,8 and 9 are not. Write a function that determines if a number is prime.
Answers
Answered by
1
Explanation:
import java.util.Scanner;
public class Prime
{
int prime(int n){
int ct=0;
for(int i=1;i<=n;i++){
if(n%i==0)
ct++;
}
if (ct== 2)
System.out.println(n+" is a prime number");
else
System.out.println(n+" is not a prime number");
return 1;
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int a=sc.nextInt();
Prime obj=new Prime();
obj.prime(a);
}
}
Similar questions