crystalfish0318
asked on
use while loop to output the sum of all even numbers
i need to prompt the user to input 2 integers: firstNum & secondNum (firstNum must be less than secondNum).
but my main problem is that i got the unexpected output when i try to output the sum of all even numbers between these 2 integers.
Here's my codes:
import java.io.*;
import java.util.*;
public class EvenNumber
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.i n));
public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n, sum, rem;
n = 0;
sum = 0;
System.out.print("please enter an integer: ");
System.out.flush();
firstNum = Integer.parseInt(keyboard. readLine() );
System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();
secondNum = Integer.parseInt(keyboard. readLine() );
rem = firstNum%2;
while(firstNum < (secondNum - 2))
{
if(rem == 0)
{
{
firstNum = firstNum + 2;
n++;
{
sum +=firstNum;
}
}
System.out.print(sum);
}
else
{
{
firstNum = firstNum + (2*n + 1);
n++;
{
sum += firstNum;
}
}
System.out.print(sum);
}
}
}
}
i input 3 and 17, then the output should be 47,
but it outputs "4112372".
please help me to check my codes.
thanks
but my main problem is that i got the unexpected output when i try to output the sum of all even numbers between these 2 integers.
Here's my codes:
import java.io.*;
import java.util.*;
public class EvenNumber
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.i
public static void main(String[] args) throws IOException
{
int firstNum, secondNum, n, sum, rem;
n = 0;
sum = 0;
System.out.print("please enter an integer: ");
System.out.flush();
firstNum = Integer.parseInt(keyboard.
System.out.print("please enter another one that is larger than the first integer: ");
System.out.flush();
secondNum = Integer.parseInt(keyboard.
rem = firstNum%2;
while(firstNum < (secondNum - 2))
{
if(rem == 0)
{
{
firstNum = firstNum + 2;
n++;
{
sum +=firstNum;
}
}
System.out.print(sum);
}
else
{
{
firstNum = firstNum + (2*n + 1);
n++;
{
sum += firstNum;
}
}
System.out.print(sum);
}
}
}
}
i input 3 and 17, then the output should be 47,
but it outputs "4112372".
please help me to check my codes.
thanks
Better with two pairs of curly braces:
int accumulator = 0;
while (++firstNum < secondNum) {
if (firstNum % 2 != 0) {
System.out.println(firstNu m);
}
else {
accumulator += firstNum;
}
}
System.out.println(accumul ator);
int accumulator = 0;
while (++firstNum < secondNum) {
if (firstNum % 2 != 0) {
System.out.println(firstNu
}
else {
accumulator += firstNum;
}
}
System.out.println(accumul
ASKER
I input 3 and 17, then the output should be 47
but it shows "70".............
but it shows "70".............
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
int accumulator = 0;
while (++firstNum < secondNum) {
if (firstNum % 2 != 0)
System.out.println(firstNu
else {
accumulator += firstNum;
}
}
System.out.println(accumul