Home Java Beginners Prime Number in Java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Prime Number in Java

Advertisement
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.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Ask Questions?    Discuss: Prime Number in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
hassan bakri
June 30, 2011
do thanks

it's good for Professionals but not to beginners can you type an easy one (just by using for loop that is test if the Entered number is prime or not ) And thanks
Nagaveni katakar
August 25, 2011
Commets

I like this tutorial its very much helpful for us. Weldone. Keep it up
kusum sorout
August 27, 2011
java programs

what is the difference between buffer reader class and scanner class??
srilatha
August 31, 2011
prime nubers

can you explain this program
Athreya
September 16, 2011
Hello

How to find the sum of all the prime numbers between 1 to 100?
Mehta Vidhi
September 17, 2011
Java programming

ok , i will try this program
vijaya
February 1, 2013
give me program for prime

please give program for a number is prime number or not if not means print previous prime number.
soma nageswararao
October 22, 2011
prime number or not in java

programing is currect,but programing only files concepts we will apply. general programing any concept will be the apply programing is currect. ok THANK YOU.
chandra
November 1, 2011
how it will works

i need clean explaination how this program will be executed?
siva
December 12, 2011
sug

it's better to give out put
editha maritoņa
January 6, 2012
hmmmmm

i dont knw how to comments,but i want to loearn more About java programs
pinky
January 19, 2012
java

sooo goood program
smithzz
January 28, 2012
Program contains errors

This program may not show the results when the input number is 2. But I think it can be solved by changing the two for loopings for (i=1; i <= num; i++ ) and for (j=2; j<i; j++). This is my suggestions. With best wishes.
Orlan Villarino
March 30, 2012
Exam code java...

how am i going to run the example code of sorting
Orlan Villarino
March 30, 2012
Exam code java...

how am i going to run the example code of sorting
mahesh
April 29, 2012
prime number

veri nice
J.ravi kumar
June 2, 2012
core java and advance java

excellent work
ANAND KUMAR YADAV
July 2, 2012
java

good solution
Tasnim
August 17, 2012
java

Sir, I am studing in MCM II but stil i don't have any cammand on java and i want to learn java very well. I kindly request to you plz guide me .
Welile
September 25, 2012
Technical programming

in this program you must typed in the whole numbes finishing when the negative number typed in. For each integer entered, the program should also print outthe number of primes found
Faisal khan
October 21, 2012
how does this work?

how does this code work? i mean it's ok but explain it how the code will run? qhats the method behind it? u got my point right?
calm
April 14, 2013
help

if (n==0){ break; } what does it do?
subu
July 15, 2012
prime

import java.io.*; import java.util.Scanner; class prime { public static void main(String args[]) {Scanner sc=new Scanner(System.in); System.out.print("enter the value="); int p=sc.nextInt(); if((p==2) || (p==3) || (p==5)) { System.out.println("the value is prime no."); } else if((p%2)==0||(p%3)==0||(p%5)==0) { System.out.println("the value is not prime no."); } else System.out.println("the value is prime no."); } }
DMCA.com