1

For some reason when I test this, the second if statement always prints every number between one and the number the user input as not prime, even if it is. However, the third if statement correctly states if the user's number is prime or not. Is there something wrong that I am doing?

public static void main(String[] args) {

    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in) ;

    System.out.println("intput a number") ;
    int number = input.nextInt() ;

    int counter = 0 ;
    int counter2 = 0 ;

    for (int i = 1 ; i <= number ; i++) {
        for (int j = 1 ; j <= i ; j++) {
            if (i % j == 0 ) {
                counter ++ ;
            }
            else if (i%j != 0) {
            }
        }
        if (counter != 2) {
            System.out.println( i+" is not prime") ;
        }
        if (counter == 2) {
            System.out.println(i+", is a prime") ;
        }
        System.out.println("\n") ;
        if (number % i == 0) {
            counter2 ++ ;
        }
    }
    if (counter2 != 2) {
        System.out.println( number+" is not prime") ;
    }
    else if (counter2 == 2){
        System.out.println( number+" is a prime") ;
    }
}
Benji Vesterby
  • 574
  • 5
  • 21
Solaire
  • 21
  • 7
  • 2
    *"lists the prime numbers between that number"* How can there be anything "between" a single number? Don't you need two numbers for "between" to have meaning? – Andreas Mar 23 '17 at 20:34

2 Answers2

1

Your program seems a little over-complicated, why not just make a method to determine if its prime or not?.

Example:

public static boolean isPrime(int number){
    if(number <= 1) return false;
    for (int i = 2; i < number; i++){
        if (number % i == 0) return false;
    }
    return true;
}

inside your main method, simply call it like this:

Scanner input = new Scanner(System.in) ;
System.out.println("input a number") ;
int number = input.nextInt() ;
for (int i = 1 ; i <= number ; i++) {
     if(isPrime(i)){
        System.out.println( i+" is a prime") ;
     }else{
        System.out.println( i+" is NOT a prime") ;
     }
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    You can replace i <= number with i <= sqrt(number). Mathematically, if a number is prime, it will not have a divisor between its own square root and 1. Any numbers after the square root are redundant. – dunck Mar 23 '17 at 20:40
  • in my computer science course we still haven't learned about returns and booleans , so if I put it in my code ill probably lose some marks or something – Solaire Mar 23 '17 at 21:17
  • @CheeseHacker no problem, you didn't mention hence I assumed you were doing it for fun I guess. – Ousmane D. Mar 23 '17 at 21:18
0

You are using the same counter for all the numbers but you are not reseting it, so the counter's value is going up and up.

int counter;
int counter2 = 0 ;

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

    //Resets the counter
    counter = 0 ;

    for (int j = 1 ; j <= i ; j++) {
        if (i % j == 0 ) {
            counter ++ ;
        }
        else if (i%j != 0) {
        }
    }

    //You don't need two if's if one is the negation of the other
    if (counter != 2) {
        System.out.println( i+" is not prime") ;
    }
    else{
        System.out.println(i+", is a prime") ;
    }
    System.out.println("\n") ;
    if (number % i == 0) {
        counter2 ++ ;
    }
}
//You don't need two if's if one is the negation of the other
if (counter2 != 2) {
    System.out.println( number+" is not prime") ;
}
else{
    System.out.println( number+" is a prime") ;
}

Also you could avoid headaches if you made an isPrime function to tell you if the numbers are prime or not, it would look cleaner.

Sacha
  • 245
  • 2
  • 10