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