Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello don\'t answer with your handwriting please this question is related to jav

ID: 3831317 • Letter: H

Question

Hello

don't answer with your handwriting please

this question is related to java

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. On the other hand, a composite number is a natural number greater than 1 that is not a prime number. For example, 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. Write a java method is PrimeNum that takes the number n, and checks whether the number is prime or composite. The method should print at the end a message that indicates whether n is prime or composite.

Explanation / Answer

Please find the solution in java :

public class TestPrime {

    public void isPrimeNum(int n){
       int i,m=0,flag=0;  
          m=n/2;  
          for(i=2;i<=m;i++){  
           if(n%i==0){  
           System.out.println("Composite number");  
           flag=1;  
           break;  
           }  
          }  
          if(flag==0)  
          System.out.println("Number is prime");  
       }
  
   
    public static void main(String a[]){
        TestPrime tp = new TestPrime();
        tp.isPrimeNum(19);
      
      
    }
}

output :

Number is prime

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote