Prime Number Finder In Java
Kushal Paudyal December 12th, 2009.This post has 200 views
A prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself.
This small program runs through 2-500 and print all the prime numbers in that range.
Note: The number 1 is by definition not a prime number.
package com.kushal.utils;
public class PrimeNumberInJava {
/**
* Method to test whether a number is
* a prime number or not
*/
public static boolean isPrimeNumber(int number) {
boolean prime = true;
int limit = (int) Math.sqrt(number);
for (int i = 2; i <= limit; i++) {
if (number % i == 0) {
prime = false;
break;
}
}
return prime;
}
public static void main(String[] args) {
/**
* Loop From 2-500 and Print all the prime numbers
*/
System.out.println("List Of Prime Numbers From 2-500");
for (int i = 2; i <= 500; i++) {
if (isPrimeNumber(i))
{
System.out.println(i);
}
}
}
}
———————————
Here is the output of this program:
List Of Prime Numbers From 2-500 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!
Originally posted 2009-05-14 14:30:10.
- Java Utilities
- Comments(0)
