Tuesday, July 1, 2008

Java Prime Number Example

What is Prime Number?

Definition : A prime number is a natural number that has only two divisor. Either it is divisible by 1 or itself and it is greater than 1. So we can say it has only one divisor means it can only divided by itself.

Here is the example of prime number in Java. It display all the prime numbers between 1 and the limit entered by user. Get the source code of prime number example here :

import java.io.*;
class Primenumberexample {
public static void main(String[] args) throws Exception{
BufferedReader bf = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Enter Limit to Print Prime Number :");
int k = Integer.parseInt(bf.readLine());
System.out.println("Prime Numbers Are : ");
for (int i=1; i < k; i++ ){
for (int j=2; j < i; j++){
int a = i%j;
if (a==0){
if(i == j){
System.out.print(" "+i);


L e o N said...

wow ur so pro in java..so can i ask u some java question coz i facing a lotsSSS of problem now T_T

L e o N said...

so i hope u can add me in msn thanks leonlai_9@hotmail.com