/*
This class check the prime number between 1 to 1000 and write the prime number.
*/
public class PrimeNumber
{
// This method tests whether a given number is prime or not.
public static boolean isPrime ( int num )
{
boolean prime = true;
int limit = (int) Math.sqrt ( num );
for ( int i = 2; i <= limit; i++ )
{
if ( num % i == 0 )
{
prime = false;
break;
}
}
return prime;
}
public static void main ( String[] args )
{
// This loop writes out all the prime numbers less than 1000.
for ( int i = 2; i <= 1000; i++ )
{
if ( isPrime ( i ) )
System.out.println ( i );
}
}
}
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
About the author
Faisal Basra
Faisal Basra is an independent consultant, software developer, writer, blogger, speaker, architect and technology leader in Lahore, Pakistan. He has been a professional software developer since 2008, has been writing code since 2006.
Having hands on experience of popular Java EE frameworks & technologies like JSF, Spring, Hibernate, Enverse, JPA, Richfaces, Primefaces, JSP/Servlet.
I have taken many initiatives while working with teams. Some of includes Automated Build & Release Management system via Hudson, Maven, Archiva & SVN.
Blogging is my hobby and I also initiated blog at corporate level from setting up complete blog for company, content generation strategy and visibility over the Internet by Internet Marketing.
Framworks & Technologies:
JSF, Richfaces, Primefaces, Openfaces, Struts, Hibernate, Spring, ORMLite
Tools & Servers:
jUnit, Log4j, Maven, Eclipse, MyEclipse, NetBeans, Tomcat, Jboss, WebLogic
Mobile Development:
Google Android
Marketing:
Internet Marketing, Mobile App Marketing
Permanent link to this article: http://www.javaplex.com/blog/java-generating-prime-number/
Leave a Reply