66.6k views
2 votes
Sum of Primes JAVA project

read 2 strings from the command line; each string will contain an integer. let's call those integers, num1 and num2. write a loop that goes from num1 and num2 looking for prime numbers. if a number if prime then store it in an array of integers, pass this array to a method that returns the sum of all the numbers in the array; you will also need to tell this method how many numbers are in the array. output only the sum of primes. you may assume a maximum of not more than 1000 numbers in the array.
*CODE*
import java.util.*;
class SumPrimes {
// go thru the array primes which has size elements
// total the integer values in primes
// return this total
// the first argument is the array of prime numbers
// the second argument is the number of primes in this array
public static int sumArray(first argument, second argument) {
}
// this method takes an integer, n, and determines if it is prime
// returns true if it is and false if it isn't
// a number is prime if it is only evenly divisible by 1 and itself
// 1 is NOT considered prime
static boolean isPrime(int n) {
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++)
if (n % i == 0)
return false;
return true;
}
public static void main(String[] args) {
// define the constant MAX which represents the largest size of the primes array
int num1, num2, numPrime = 0;
// declare an array of the proper data type and size
// convert 1st command line argument from a string to an integer
num1 = Integer.parseInt(args[0]);
// get num2 from the command line
// loop through all integers from num1 to num2
// store those that are prime in the primes array
// keep track of the number of primes - numPrime
// output the sum of all the primes
System.out.println(sumArray(primes, numPrime));
}
}

User Anda B
by
5.8k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

The following code is written in Java and creates the necessary code so that two int inputs are read and looped through all the numbers between them to find all the primes. These primes are counted and summed. Finally, printing out the sum and the number of primes found to the console.

import java.util.ArrayList;

import java.util.Scanner;

class SumPrimes{

public static void main(final String[] args) {

Scanner in = new Scanner(System.in);

ArrayList<Integer> primes = new ArrayList<>();

int num1, num2, numPrime = 0;

System.out.println("Enter number 1:");

num1 = in.nextInt();

System.out.println("Enter number 2:");

num2 = in.nextInt();

for (int x = num1; x <= num2; x++) {

if (isPrime(x) == true) {

primes.add(x);

numPrime += 1;

}

}

System.out.println("There are " + numPrime + " primes in the array.");

System.out.println("Their sum is " + sumArray(primes));

}

public static int sumArray(ArrayList<Integer> arr) {

int sum = 0;

for (int x: arr) {

sum += x;

}

return sum;

}

static boolean isPrime(int n) {

if (n <= 1)

return false;

for (int i = 2; i < n; i++)

if (n % i == 0)

return false;

return true;

}

}

Sum of Primes JAVA project read 2 strings from the command line; each string will-example-1
User Rob Cowell
by
5.7k points