Welcome to our free-to-use Q&A hub, where students post questions and get help from other students and tutors.

Follow the trail of responses and if you have anything to add please sign up or sign in.

You can ask your own question or look at similar Computing questions.


public class PrimeExample{    

 public static void main(String args[]){    

  int i,m=0,flag=0;      

  int n=3;//it is the number to be checked    

  m=n/2;      

  if(n==0||n==1){  

   System.out.println(n+" is not prime number");      

  }else{  

   for(i=2;i<=m;i++){      

    if(n%i==0){      

     System.out.println(n+" is not prime number");      

     flag=1;      

     break;      

    }      

   }      

   if(flag==0)  { System.out.println(n+" is prime number"); }  

  }//end of else  

}    

}   

Hi Aiesha,

This answer assumes you are capable of writing the code itself but gives you an overview of the structure needed:

First set up the program parameters (class etc.)
Then define an integer which is to be the number to be checked, you can define it as a number in the code or as a user input.
Attempt to divide the number by 2, 3, 4 etc, using a iteration to increase the value of the number you are dividing by each time.
At the end of each loop, have the program check if the new number is an integer and is not 0 or 1. If this is true then have the program print that it is not a prime number and kill the program.
If the output division equals 1 then have it read that it is a prime (since the loop must have increased the number to divide up to the value of the input number, meaning it is a prime).

Hope that helps, I can give you a lesson on prime numbers if that would help.

Kind regards,

Tom

Aiesha if you need a clue its find a code equivalent for the operation of proving prime.

Eg, (sorry again a little rusty code but here goes) when 'n' not 0; "prime" if 'n' divides by 1 and n only. Perhaps another line or 2 to cover all primes technically but that the point is, is a very accurate way to code a prime programme where 'In Alumni' it is very much a taboo area to be so confident as to technically be able to 100% get correct prime programme where there are many real logical arguments to say most common ways to code are evidently incorrect. 

Other areas which are unique and fascinating by fact in HE / Honorary study Aisha is the area of Dentistry and Optology where it is still decided by institute not award what exactly the level of Profession is with regards to medicine. IE, you may be an Dental Surgeon with a very low salary indeed or an Optologist (eye expert) without any sort of medical education.

I mention this as it seems that you will not be a student forever on here wheras as a tutor I am and will be answering questions about Prime number programme or advanced calculus in my opinion limitlessly and it is important that you are referred to directions which will evidently be important for your level of education desired and professional employability in the long term. All the best, M Cover SE and Cambs.

Footer Graphic