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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Prime Number in Java 
 

This Java programming tutorial will be read how to get prime number. First of all we will define a class "Prime Number".

 

Prime Number in Java

                         

This Java programming tutorial will be read how to get prime number. First of all we will define a class "Prime Number". 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 two for loop conditions first condition  as integer i=1, i< num and i++ is increment and second condition with as integer j=2, j<i, or j++ increment operator. So output result will be like int n=i%j.  

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.

                         

» View all related tutorials
Related Tags: java c ide array class div io matrix get vi this id define ping for program to learn ram ear

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
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

Indian Software Development Company | iPhone Development Company in India | Flex 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.