Leo li
Leo li

Reputation: 13

A loop for checking prime number

package pureTest;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class test3 {    
        public static void main(String[] args) {
            /* Enter your code here. */
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            for (int i = 2; i< n; i++){
              if( n <= 3){
                  System.out.println("Prime");
                  break;
              }else if( n%i ==0){
                   System.out.println("Not Prime");
                   break;
              }else{
                  System.out.println("Prime");
              }
            }

          }
      }

the input of 7; the out put is repetitions of Prime:

7
Prime
Prime
Prime
Prime
Prime

Just wondering why the if condition doesn't work out here.

Upvotes: 0

Views: 2692

Answers (4)

Ali
Ali

Reputation: 1590

your code will print prime until it finds divisor !

for (int i = 2; i< n; i++){
          if( n <= 3){
              System.out.println("Prime");
              break;
          }else if( n%i ==0){
               System.out.println("Not Prime");
               break;
          }else{
              System.out.println("Prime"); --> this line will be printed every time in your loop!
          }
        }

Also you don't need to iterate till n, as after n/2 there would be no number which can divide n :-)

Check this code...

private static boolean checkPrime(int n) {
    int i = 2;
    while(i<=n/2){
        if(n%i++ == 0){
            return false;
        }
    }
    return true;
}

Upvotes: 1

Kranthi
Kranthi

Reputation: 11

boolean isPrime = true;
    Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
    for (int i = 2; i< n; i++){
        if(n%i==0) {
          System.out.println("Not Prime");
          isPrime=false;
          break;
       }
    }

    if(isPrime) {
       System.out.println("Prime");
    }

Upvotes: 0

xenteros
xenteros

Reputation: 15862

public static void main(String[] args) {
        /* Enter your code here. */
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        boolean isPrime = true;
        for (int i = 2; i< n; i++){
          if( n <= 3){
              break;
          }else if( n%i ==0){
               System.out.println("Not Prime");
               break;
          }
        }
        if (isPrime) {
            System.out.println("Prime");
        } else {
            System.out.println("Not prime");
        }
      }

If you want to simply check in the main function the above is ok. If you want to do that well try the following:

public static void main() {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    if (isPrime(n)) {
        System.out.println("Prime");
    } else {
        System.out.println("Not prime");
    }
}

boolean isPrime(int n) {
    if (n < 2) {
        return false;
    if (n == 2)
        return true;
    if (n%2 == 0)
        return false;
    for (int i = 3; i*i<=n; i+=2){
        if (n%i == 0)
            return false
    return true;
}

Upvotes: 0

Eran
Eran

Reputation: 394156

Your else clause is wrong. It prints "Prime" each time n is not divisible by i. It will even print prime for non prime inputs (for example, it will print "Prime" for 21 before printing "Not Prime", since 21%2 != 0).

Change your loop to something like :

      for (int i = 2; i < n; i++) {
          if( n <= 3){
              System.out.println("Prime");
              return;
          } else if(n%i == 0){
              System.out.println("Not Prime");
              return;
          }
      }
      System.out.println("Prime");

Upvotes: 1

Related Questions