3:53 pm - Sunday March 25, 2029

Java program for Find the Prime Numbers

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

In basics, Prime numbers are only divided by ‘1’ and number itself. At the same time, all the prime numbers are greater than ‘1’. Here is a simple prime number program in java with an example to find prime numbers from given input.

 


/**

*@author :CandidJava.com

*@descriptions : This program  using  to find prime numbers.

*/

class Prime{

public static void main(String args[]) {

int i,n=1;

//display the prime numbers upto300

while(n<=300) {

//assigning r=0

int r=0;

//using for loop  increment the value of i

for(i=2;i<n;i++) {

//if  the remainder of n/i==0 means print r=1

if (n%i==0) {

r=1;

}

}

if(r==0) {

//If  n%i==0 means print the value of n

System.out.println("The value is"+n);

}

//increment the value of n

n++;

}

}

}

 Output:


D:\ Candidjava\Code >javac Prime.java

D:\ Candidjava\Code >java  Prime

The value is1

The value is2

The value is3

The value is5

The value is7

The value is11

The value is13

The value is17

The value is19

The value is23

The value is29

The value is31

The value is37

The value is41

The value is43

The value is47

Filed in: Z-A All Java Codes

No comments yet.

Leave a Reply