write a program in java to enter a number and check whether it. is prime or composite(check via factors)
Answers
Answered by
2
a=number to be checked
rem=remainder,c=no of factors,f=loop variable for finding factors.
if c=2(no of factors) means it is a prime no because every prime no is divisible by one and the no itself only and a composite number always has other factors leaving 1 and the number itself resulting in more than two factors always.
PROGRAM CODE:-
import java.util.*;
public class prime_no
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,rem=0,f=0,c=0;
System.out.println("enter a no");
a=sc.nextInt();
for(f=1;f<=a;f++)
if(a%f==0)
c=c+1;
{
if(c==2)
System.out.println("it is a prime no");
else
System.out.println("it is composite no");
}
}
}
Answered by
0
Answer:
ktejfaididkyeulepirornv. m mvufutgaidh vig
Similar questions