The project :
Deisgn and implement an application that reads an integer value and prints the sum of all even integers between 2 and in the input value, inclusive. Print an error message if the input value is less than 2. Promt accordingly.

Query :
I'm not sure how I can get java to read all even numbers between 2and the number inputted, can anyone help me to how i can achieve this?

Recommended Answers

All 2 Replies

If I appear offline, it doesn't mean I'm not following this thread. Please do not be discouraged, I am not seeking an easy quick answer.

Assuming that you have read the number.
- Use an "if" to see if it is greater than 2. Remember you need the sum of numbers from 2 till the number given.
- Have a for loop for the sum:

for (int i=[B]2[/B];i<=[I][B]givenNumber[/B][/I];i++) {

}

I hope that you know how to calculate the sum of numbers using a for loop.

And as for your question use the '%' operator. If:
D = a*b + y. Then:
D%a = y
D%b = y

So for even numbers use this:

if (number%2==0) {
  // number is even
}

Since the above if returned 0 it means that it can be divided by 2, so it is an even

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.