Akarlsen -4 years ago 94

Java Question

Can't find out how to go further when working with two unknown integers in parameters. Have several tasks regarding this.

- returning the sum between two unknown integers, the two unknown numbers have to be included in the calculation as well
- returning the even numbers between the two unknown
- returning the odd numbers between the two unknown
- returning the prime numbers between the two unknown

I know this probably is very easy, but still can't find out how to get started.

`public int method(int a, int b){`

sum = 0;

??for() {

}??

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
public static void main(String[] args) {
System.out.println("sum: " + sum(10, 12));
System.out.println("evens: " + evens(10, 20));
System.out.println("odds: " + odds(10, 20));
System.out.println("primes: " + primes(0, 100));
}
public static int sum(int from, int to) {
int sum = 0;
for (int i = from; i <= to; i++) {
sum += i;
}
return sum;
}
public static List<Integer> evens(int from, int to) {
List<Integer> evens = new ArrayList<>();
if (from % 2 != 0) {
from++;
}
for (int i = from; i <= to; i += 2) {
evens.add(i);
}
return evens;
}
public static List<Integer> odds(int from, int to) {
List<Integer> odds = new ArrayList<>();
if (from % 2 == 0) {
from++;
}
for (int i = from; i <= to; i += 2) {
odds.add(i);
}
return odds;
}
public static List<Integer> primes(int from, int to) {
List<Integer> primes = new ArrayList<>();
boolean prime = false;
for (int i = from; i <= to; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
return primes;
}
public static boolean isPrime(int n) {
for(int i = 2; 2 * i < n; i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**

Latest added