(adsbygoogle = window.adsbygoogle || []).push({}); 1. The problem statement, all variables and given/known data

Write a program that takes a command line argument N and a sequence of N positive integers and prints the numbers that are prime only, followed by their sum.

2. Relevant equations

for loop

3. The attempt at a solution

This has been a vexing program to think of. We were asked to write a program that takes an integer N and outputs true if N is prime and false if N is not prime. The code is as follows:

Although we haven't actually covered the break command, but I didn't know how to code it without it.Code (Text):public class Primes

{

public static void main(String[] args)

{

int numb = Integer.parseInt(args[0]);

int i;

boolean ifPrime;

for (i = 2; i < numb; i++)

{

int n = numb % i;

if (n == 0)

{

ifPrime = false;

System.out.println(numb + " is not a prime number, hence " + ifPrime);

break;

}

}

if (i == numb)

{

ifPrime = true;

System.out.println(numb + " is a prime number, hence " + ifPrime);

}

}

}

Now, the question above is a little confusing to me, particularly the wording. Does it want to take a command-line value N then N numbers afterward or is it something else?

The way I thought of it is to first write a method of type boolean that studies the integer(s) and then returns true or false if prime or not. We enter a loop in the main method that loops over argswhere i is an initialization variable (int), then use if to compare the returned boolean value if. But here's where I hit the wall. What's next? How do you continue from here?

This is my last question, I hope.

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Sum of prime numbers taken from the command-line

**Physics Forums | Science Articles, Homework Help, Discussion**