For Loop

Prime Numbers Java Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

40 Comments

  • In the source code of Finding prime numbers I wanna suggest some correction
    i.e. in the inner for the check condition should be for(int j=2;j<=(i/2);j++);
    This will reduce extra iterations that are not required —Amarjeet(TYIT 07629 College GPKP Kolhapur Maharashthra. Thank You!

  • Where they wrote the number 100 in the for loop, they could have replaced it by typing in the variable “limit” which they have created and initialized yet not used to it’s full potential!

    • in place of 100 you can put limit.. also if you want to set your own limit then you can also use scanner class to use it.

  • Hi,
    I found two mistakes here:

    1. Line 17 should be :

    for(int i=2; i < 100; i++){ // 1 is not a prime.

    2. Line 22 should be replaced by next two lines:
    int endLimit = (int) Math.sqrt(i);
    for(int j=2; j <= endLimit ; j++){ //is enough to divide up to sqrt of the number

    I have quite faster algorithm, but the space here is too small to show it. It is not nessessary to divide by all numbers up to the square root of the number. It’s enough to divide only by primes up to the square root of the number.

    Regards,
    Alexander Papukchiev

  • find and print all prime number between 3 and 100 your program should use a method called isprime that returns a boolean value indicating whether its integer parameter is a prime number or not ..

    • This, will give you a 3 and 5 though..
      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

      if(isPrime){
      int alterna=0;
      if(alterna%2!=1){
      System.out.print(i + ” “);
      }
      else{
      alterna+=1;
      }

  • public class MyPrimeNumberCheck {

    public boolean isPrimeNumber(int Number){

    for(int i = 2; i<=Number/2; i++){

    if(((Number%i) == 0)){

    return false;

    }

    }

    return true;

    }

    public static void main(String[] args){

    MyPrimeNumberCheck Prime = new MyPrimeNumberCheck();

    for(int number = 1; number<=50; number++){

    //print prime numbers only

    if((Prime.isPrimeNumber(number))){

    System.out.println(number);

    }

    }

    }

    }

  • *********
    The best way to find the Prime numbers between 2 to 100
    *********

    public class PrimeNumbers {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    String s=” “;

    for( int num=2;num<=100;num++) {

    boolean flag=true;

    for(int div=2;div<=9 && div < num;div++) {

    if(num%div == 0) {

    flag=false;

    break;

    }

    }

    if(flag)

    s=s+num+” , “;

    }

    System.out.println(s);

    }

    }

  • Hi friends I have a question ….how can I recognize the number that I import to the console is prime or non prime????jus with consol input help me thanks

  • 1 is not a prime number. A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. Update the program.

  • public class PrimeNumber {

    public static void main(String args[]) {
    int divisible =2, i=1, limit =100;
    System.out.println(“prime number between 1 to “+ limit);
    while (i<limit) {=”” if(i=”” %=”” divisible=”=0)” {=”” system.out.println(i+=”” “is=”” not=”” prime=”” number”);=”” }=”” else=”” {=”” system.out.println(i+=”” “is=”” prime=”” number”);=”” }=”” i++;=”” }=”” }=”” }=””>

  • import java.util.Scanner;

    class Prime

    {

    public static void main(String[] args)

    {

    Scanner sc=new Scanner(System.in);

    System.out.println(“enter the number “);

    int k=sc.nextInt();

    for (int i=2;i<k ;i++=”” )=”” {=”” if=”” (i=”=2|i==3|i==5|i==7)” {=”” system.out.print(i+”=”” “);=”” }=”” else=”” if=”” (i%2=”=0|i%3==0|i%4==0|i%5==0|i%6==0|i%7==0|i%8==0|i%9==0)” {=”” }=”” else=”” system.out.print(i+”=”” “);=”” }=”” system.out.println(“hello=”” world!”);=”” }=”” }=””>

  • why dosn’t it work with the nr 22 :

    import java.util.Scanner;

    public class Aufg3 {
    public static boolean istPrimzahl(int n){
    boolean b=false;
    if(n<=1){
    b=false;
    }
    if(n==2){
    b=true;
    }
    else{
    for(int x=2;x<=Math.sqrt(n);x++){
    if(n%x==0){ //wenn es durch irgendwass teilbar ist
    b=false;
    }
    else{
    b=true;
    }
    }
    }
    return b;
    }

    public static void main(String[] args) {
    int n;
    Scanner in = new Scanner(System.in);

    System.out.println(“Geben Sie eine Zahl ein:”);
    n=in.nextInt();
    System.out.println(“Ist es eine Primzahl?”);
    System.out.println(istPrimzahl(n));
    }

    }

    output:
    Geben Sie eine Zahl ein:
    22
    Ist es eine Primzahl?
    true

    Can someone explain why it dosn’t work for 22?

Sponsors

Facebook Fans