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

Search:
   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
Prime Number in Java
In this section, you will learn how to get prime number between 1 to given number.
 
 

Prime Number in Java

                         

This Java programming tutorial, we will be read how to get prime number between 1 to given number. First of all we have to define a class "PrimeNumber". Java I/O package has a input stream and a output stream in which input stream is used for reading the stream and memory allocating and the output stream used for writing bytes. As in this program we are going to insert certain instruction by creating buffer reader class. Here we have to create a buffer for the string class that can be used to instantiate a changeable object for storing and processing a string of character. Now use the ParseInt method for converting the parses the string argument and define 'num' as an integer.

Now applying in this program we use two 'for' loop. For loop will start from 1 to entered number. And another loop will start and divide it from 2 to less than those number. If number is divided by any number that means it is not prime otherwise prime number.

Here is the code of the Program

import java.io.*;

class PrimeNumber {
  public static void main(String[] argsthrows Exception{
    int i;
    BufferedReader bf = new BufferedReader(
             
new InputStreamReader(System.in));
    System.out.println("Enter number:");
    int num = Integer.parseInt(bf.readLine());
    System.out.println("Prime number: ");
    for (i=1; i < num; i++ ){
      int j;
      for (j=2; j<i; j++){
        int n = i%j;
        if (n==0){
          break;
        }
      }
      if(i == j){
        System.out.print("  "+i);
      }
    }
  }
}

 Download this Example.

                         

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

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

Couldn't understand the code where it says:
if(i == j)
Can anyone explain me this bit?y do we need that for?

Posted by sal85 on Saturday, 09.6.08 @ 08:43am | #77419

Actually Connection is an interface in jdbc,without creating a class how can we get a method inside connection interface?

Posted by neelakandan on Monday, 08.25.08 @ 11:28am | #74972

regarding bufferreader-is it able to use datainputstream instead.

really this tuts help me a lot!

nith wishes----------naveen

Posted by naveen on Monday, 03.24.08 @ 10:17am | #54032

I really respect who has developed this website and uploaded the easy and good programs. i really like this webiste and due to this website i have got A+ in my final exams in java. i have not touched even my course book.
I really like it keep it up and please upload the easy codes for asp.net also.
Best Regards,
Mirza
IT Administrator

Posted by Mirza on Wednesday, 08.8.07 @ 13:25pm | #22922

I cant understand what "throws Exception" do

Posted by heba on Saturday, 04.21.07 @ 23:20pm | #14696

how to write given no is prime or not plz provide code for that

Posted by shivaji on Friday, 03.30.07 @ 12:47pm | #12977

ok i solved the prob

Posted by kasia on Thursday, 03.8.07 @ 03:59am | #11063

tutorials so far word fine till i got 2 this one and
i get an error on line 18
int num = Integer.parseInt(bf.readLine());

with bf.readLine();

Posted by kasia on Thursday, 03.8.07 @ 03:49am | #11061

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.