Computer Science, asked by manishkumarthap65j0u, 1 year ago

Write a JAVA program to check whether a number is prime or composite or 1.

Answers

Answered by Akshaymas
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.

Answered by uashenoy05
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
Similar questions