Write a JAVA program to check whether a number is prime or composite or 1.
Answers
Answered by
3
public boolean isPrimeNum (int n) { boolean isPrime= true; for( int i=2; i<= n; i++) { if( n%i==0) { isPrime= false; } isitPrime= true; if(isPrime) System.out.println("its a prime number"); else System.out.println("its a composite number"); }
I have attempted to write this method to check whether a number is prime or composite, but I am not sure whether it is correct. Any kind of help would be appreciated and thanks in advance.
I have attempted to write this method to check whether a number is prime or composite, but I am not sure whether it is correct. Any kind of help would be appreciated and thanks in advance.
Answered by
18
HEYA>>>>
____________________________________
import java.util.*;
public class prime{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int a,n,c=0;
System.out.println("enter a number");
n=in.nextInt();
for(a=1;a<=n;a++){
if(n%a==0)
c=c+1;}
if(c==2)
System.out.println("the number is prime")
else
System.out.println("the number is composite");}
}
____________________________________
hope it helps
____________________________________
import java.util.*;
public class prime{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
int a,n,c=0;
System.out.println("enter a number");
n=in.nextInt();
for(a=1;a<=n;a++){
if(n%a==0)
c=c+1;}
if(c==2)
System.out.println("the number is prime")
else
System.out.println("the number is composite");}
}
____________________________________
hope it helps
Similar questions
English,
9 months ago
Social Sciences,
9 months ago
Hindi,
9 months ago
Math,
1 year ago
Math,
1 year ago