Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

Find out the prime number

In this section , you will learn how to find out whether a given number is prime or not.

Find out the prime number

                         

This lesson of Java programming language will teach you the coding to find out whether a given number is prime or not. Here we have used the 'for loop' statement and given the required condition for a prime number. As we know, a prime number is only divided by 1 and itself, in other words it has no other factorial other than 1 and the number itself.  

Here, first make a class and named as "Primenumber" and take an integer as num=11, and define an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num. Now apply this in the "for loop" statement and define an integer n=num/i as given below in the example. Now apply the "if" condition and if the reminder of the earlier equation comes "0", then the result will be not prime. Again the loop system will check the above condition until it has not satisfied from the starting point(2) to the end(10). Here under this loop we have to use the "break" statement for unnecessary checking further one point where the reminder comes zero(0). 

Now after checking the whole condition, if the reminders does not come "zero", then we have to again apply the "if" condition and check whether i=num or not. If it is true then number (num) is prime. As we have taken here as num=11, then after compiling and running the program, the result will show that num is prime number.  

Here is the code program: 

class Prime_number {
  public static void main(String[] args) {
    int num = 11;
    int i;
    for (i=2; i < num ;i++ ){
      int n = num%i;
      if (n==0){
        System.out.println("not Prime!");
        break;
      }
    }
    if(i == num){
      System.out.println("Prime number!");
    }
  }
}

Download this page

                         

» View all related tutorials
Related Tags: c com class date comparison io methods method dates using equals this for ssi e iso section not ls first

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

7 comments so far (
post your own) View All Comments Latest 10 Comments:

can any one help me to give a program in micro controller for checking whether a given number is prime or not ,,,,,,,,,,,,,urgent

Posted by jerin on Friday, 10.3.08 @ 20:58pm | #80877

I have this Prime Checking Java coding problem:

A prime number ‘p’ is a positive integer (p>1) that can only be divided evenly by:
a) ‘p’ Itself (so the result of the division is: p/p = 1)
b) 1 (so the result of the division is p/1 = p)

The first few prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31

Write a Class PrimeChecker that has checkPrime() method as follows:

public class PrimeChecker {

public static boolean checkPrime(int anyNumber){

// write your logic here
}

public static void main(String[] args) {
boolean found = false;
for (int p=-5; p<35;p++){
found = checkPrime(p);
if (found){
System.out.print(p + "\t");
}
}
}
}

Obviously, the method checkPrime will take in any integer and return true of false depending on whether integer is Prime or not.

As a tester for your code, the main method is sending a series of integers to the checkPrime method.

Here is the expected output:

2 3 5 7 11 13 17 19 23 29 31

Thanks.

Posted by Cris on Monday, 03.10.08 @ 04:58am | #52148

thanks for ur help

Posted by paddu on Thursday, 09.20.07 @ 19:04pm | #28204

This is a very simple implementation of a prime number check. I feel checking every single divisor of a number to verify its primality is sort of 'brute force' is there a more elegant way to do this?

Posted by Saleem on Thursday, 05.10.07 @ 15:08pm | #15568

how to develop java progrmas logic

Posted by saikrishna on Sunday, 03.25.07 @ 16:35pm | #12661

i found very interesting and easy to understood your explanations and i think you're doing a terrific job.

as all of beginners, i need a little help from u:
what if i have more numbers i have to find the prime one, how can write the code?

thank u for your time and best regards
PDE

ps- hope u have time for my question

Posted by PDE on Thursday, 01.11.07 @ 05:45am | #3169

wow! this is really cool. an easy program that is. can you tell me what the % do? does this really compute all prime numbers including above million? becuase I tried to input more than a million and it always come out to be "not prime," I'm just not sure if it's really accurate. is it?

do you have a program for a simple calculator that computes 3 operations, as for example 89+34*6 - in this case, it follows the math rule which executes the multiplication first then addition?

well, i just need a little help in starting my project. and i hope you would be able to read this and consider my request.

thanx.

Posted by Markhooz on Monday, 12.11.06 @ 08:19am | #745

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.