I am doing a primesum program. The aim of this program is to find the sum of prime integers. I have a problem here dealing with negative numbers. What should I add inside my code to ignore negative inputs? Anyone can help me?
Here is my code below. It is doing something but it does not ignore my negative inputs.
public class primesum
{
public static int checkPrime(int entry)
{
int x = entry; //Initial sum of the primes
int count = 1; //The modulus factor
while(count <= Math.sqrt(entry))
{
if(entry % count != 0)
{
count++; //Do the next modulus factor
}
else
{
x = 0;
break;
}
}
return x; //Adds all the sum up and returns the total sum
}
public static void main(String[] args)
{
int x = 0;
BufferedReader bR = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter some 32-bit integers: ");
int entry = 1;
try{
while(entry != 0){
String line = bR.readLine();
entry = Integer.parseInt(line);
x += checkPrime(entry); //Checks whether x is a prime
}
}
catch(Exception ex){
System.out.println(ex);
}
System.out.println("The sum of the primes is " + x);
}
}