3

I have this code:

public class TestPrimaryArray
{
   public static void main(String[] args)  
   {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter 5 numbers: ");
      int[] numbers = new int[5];
      for(int i = 0; i < numbers.length; i++)
      {
         numbers[i] = scan.nextInt();
         boolean isPrime = PrimeArray(numbers);
         System.out.println("Is " +numbers[i]+ " a prime? " +isPrime);
      }
   }

   public static boolean PrimeArray(int[] arr)
   {
      boolean prime = true;
      for(int i = 1; i < arr.length; i++)
      {
         if(arr[i]%2 == 1)
            prime = true;
         else
            prime = false;    
      }
      return prime;
   }
}

This program is suppose to take input from the user place it into a one dimensional array of 5, and then tell whether the numbers are prime or not. I run my code and it compiles and everything, but when I see the results it does not give me the right answer. Here is an example run with the numbers 11 7 3 5 20

Enter 5 numbers: 
11 7 3 5 20
Is 11 a prime? false
Is 7 a prime? false
Is 3 a prime? false
Is 5 a prime? false
Is 20 a prime? false

It is suppose to tell me that all of them except 20 are true. I do not know what I am doing wrong. What should I do?

3
  • Why does PrimeArray take an array if you call it for each input? And have you noticed, that you set the variable prime for each number of the passed array and return its value after the loop? So it is quite clear that it can only return the result of the last entry of your array. So please rething the PrimeArray method. Commented Oct 29, 2015 at 20:11
  • Some hints: do you really think that all odd numbers are primes? To determine if a number is a prime, why do you need all the other numbers? Commented Oct 29, 2015 at 20:12
  • why are you taking the whole array as input to method PrimeArray each time? Use first for loop to populate the array. Use second for loop in primeArray to print the output. Commented Oct 29, 2015 at 20:12

4 Answers 4

2

First, you should be testing one value at a time (since you return one boolean). Second, you should short circuit and return the first time the number is divisible. Otherwise you'll return true because you reset the value of prime. Finally, Java method names start with a lower case letter (by convention). And, your method only tests for evenness. I think you wanted something like

public static boolean isPrime(int val)
{
    for(int i = 2; i < (val / 2); i++)
    {
        if(val % i == 0)
            return false;
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're checking all elements but returning the last one, which is false (20%2 is 0, so your condition is false).

Instead you must return a boolean array, and print each one.

import java.util.Scanner;
public class TestPrimaryArray {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System. in );
        System.out.println("Enter 5 numbers: ");
        int[] numbers = new int[5];
        for (int i = 0; i < numbers.length; i++) {
            numbers[i] = scan.nextInt();
            boolean isPrime[] = PrimeArray(numbers);
            System.out.println("Is " + numbers[i] + " a prime? " + isPrime[i]);
        }
    }

    public static boolean[] PrimeArray(int[] arr) {
        boolean prime[] = new boolean[arr.length];
        for (int j = 0; j < arr.length; j++) { //Arrays start from index 0
            for(int i = 2; i <= Math.sqrt(arr[j]); i++) { //Compare until the square root of the number since it's faster and after this value you have checked if a number is prime or not.
                if(arr[j] % i == 0) {
                    prime[j] = false;
                    break;
                } else {
                    prime[j] = true;
                }
            }
        }
        return prime;
    }
}

2 Comments

"Hello, I'm number 'nine' and I'm here to tell you: I'm not a prime".
@Henry updated answer so it checks correctly if a number is prime or not
0

A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

First you should not pass the array to your function, the PrimeArray function you wrote always returns the calculated value for the last item in the array.

Second, The function you wrote does not check being a prime correctly.

Here is a change in your code:

public class TestPrimaryArray
{
   public static void main(String[] args)  
   {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter 5 numbers: ");
      int[] numbers = new int[5];
      for(int i = 0; i < numbers.length; i++)
      {
         numbers[i] = scan.nextInt();
         System.out.println("Is " + numbers[i] + " a prime? " + isPrime(numbers[i]));
      }
   }

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

Comments

0

This is not a program to check the prime numbers, you are checking whether a number is odd or even. You return false, if the array contains even one odd number.

public class TestPrimaryArray {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter 5 numbers: ");
            int[] numbers = new int[5];
            for(int i = 0; i < numbers.length; i++)
            {
                numbers[i] = scan.nextInt();
                boolean isPrime = PrimeArray(numbers[i]);
                System.out.println("Is " +numbers[i]+ " a prime? " +isPrime);
            }
        }

        public static boolean PrimeArray(int number)
        {
            boolean prime = true;

            if(number %2 == 0)
                prime = false;

            else {
                for(int loop=3;loop < Math.sqrt(number); loop ++)
                    if((number % loop) == 0)
                    {
                        prime = false;
                        break;
                    }
            }
            return prime;
        }
}

1 Comment

"You return false, if the array contains even one odd number.": No, the function returns wether the last element in the array is odd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.