Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

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

Latest Searches:
how to connect Ms acce
Tree max
difference between J2S
Photoshop Web Layouts
update data in databse
oops
blob objects
Flash Math Physics Cal
jvm memory
write a xml file using
load videos in applet
prepareStatement
two date picker diff
search page to display
GridLayout
mysql blob
POI and excel
notepad code
using check box in dat
ENUMERATE OPERATIONS O
Getparameter
core java Applets
ASP Miscellaneous Incl
Flash Text Tutorial xm
c:out in var of c:fore
Transforming iCal Cale
Photoshop Abstracts Ce
Treemap using gwt tree
nested logic
tag for stuts table
ะ ยà¸?ะ?ยà¸?à
Photoshop Textures and
message box display in
Photoshop Effects Obje
Read the Key-Value of
jpanel
spring mvc
Java Notes: Downcastin
decimal to char
hide table in jsp
java object pool
addFieldError
jsf dropdown
jsp include
hybernate annotations
read odd rows in excel
writing data to file
Photoshop Photo Retouc
BufferedOutputStream
class bean
where to keep backgro
servlet exception hand
java cancel button
HSSF clone cell
DEMO APPLICATION
Java 1.4 Tutorials
to use java class in j
relationalioperator
axis2 tomcat example
Ñ?â?£?Ñ?â???Ñ?â???Ñ?â?
export database data i
multiple log files
servlet and ajax and x
Jigloo SWT/Swing GUI B
struts2 tag iterate
java source code for i
create table in struts
download api struts
combox using in strute
OutStream wrapper Clas
struts2 enum
ะ ย ะà¸???ะÂ
s:iterate
File Java
a sample java project
BASICS OF ARRAY IN JAV
ั?ะ?ะ?ั?ะ??à
video streaming UDP
panel group
PHP Guestbooks Simple
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

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

Copyright © 2008. All rights reserved.