This code is supposed to determine for a number of random numbers whether its odd, even , prime.
At the end all random numbers should be displayed and the prime ones together with the number of odd and even numbers and their %.

The part of the code to determine if its prime doesn't seem to want to work --- after compilation while running - the error at if (randomNos % k == 0) is Aritmetic Exception " / 0 "

Heres my code:

/**
 * Write a description of class RandomNumbers here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class RandomNumbers
{
    public RandomNumbers()
    {

    }

    public boolean isEven( int randomNos )
    {
        if (randomNos % 2 == 0 )
        return true;
        else
        return false;
    }

   public boolean isPrime( int randomNos )
    {
        boolean noRemainder = false;
        for (int k = 2 ; k < (randomNos-1) | noRemainder == true; k++)
        {
            if (randomNos % k == 0)
            noRemainder = true;

        }

        if (noRemainder == true)
        return false;
        else
        return true;
    }
}








/**
 * Write a description of class testRandomNumbers here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
import java.util.Random;
import javax.swing.JOptionPane;
public class testRandomNumbers
{
    public static void main(String [] args)
    {
        Random randNos = new Random();
        int evenCounter = 0;
        int amount = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value indicating how many Numbers:"));

        System.out.println("Random \t Primes");
        for (int k =2 ; k <= amount ; k++)
        {
            String prime1;
            int number = randNos.nextInt(15)+1;
            RandomNumbers test1 = new RandomNumbers();

            boolean isEven = test1.isEven(number);
            if (isEven == true)
            evenCounter++;

            boolean prime = test1.isPrime(number);
            if( prime == true)
            prime1 = Integer.toString(number);
            else
            prime1 = " ";

            System.out.println(number + " \t " + prime1);

        }

        System.out.println("\nEven: " + evenCounter);
        double percentage = (double)evenCounter / amount * 100;
        System.out.printf("%s%.2f" , "Percent: " , percentage);
        System.out.print("\nOdd: " + (amount - evenCounter));
        double oddPerct = 100 - percentage;
        System.out.printf("\n%s%.2f" , "Percent odd: " , oddPerct);
    }


}

Please help me. I write today at 14:00

It's hard to see how you could get a divide by zero when k starts at 2 and goes up...
try putting some print statenents into that loop to show the values of randomNumber and k so you can see what's happening.

also: you don't have to go this high: (randomNos-1)
going to the half of the number 'll be just fine.

another little 'refactoring':

 if (noRemainder == true)
        return false;
        else
        return true;

why not just replace this with:

return !noRemainder;

even if you want to keep the above, it's still beter just to say:

   if (noRemainder)
        return false;
   return true;

you don't need the == true part. noRemainder is a boolean on it's own, and thus accepted as expression in the if statement.
also, you don't need the else.
unless the else would be reached, the return false has already ended the method.

google up "sieve of eratosthenes" . the algorithm results in a small but powerful code for finding primes.

Edited 3 Years Ago by somjit{}

You are getting the division by zero because this is an infinate loop:

for (int k = 2 ; k < (randomNos-1) | noRemainder == true; k++)

"k" is overflowing and eventually making its way back to zero.

Comments
Wow! Nice analysis

There's also a problem that the number "1" is not normally considered a prime number. But this program thinks that it is.

There are several schools of thought about that, Jeff.
Mathematically, 1 is indeed a prime number as such are defined as any number that can only be divided by 1 and itself. The number 1 of course meets both criteria.

This question has already been answered. Start a new discussion instead.