Link to home
Start Free TrialLog in
Avatar of crystalfish0318
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.in));
      
      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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Following from my previous answer:


          int accumulator = 0;
          while (++firstNum < secondNum) {
                if (firstNum % 2 != 0)
                             System.out.println(firstNum);
                else {
                      accumulator += firstNum;
                }
          }
          System.out.println(accumulator);
Better with two pairs of curly braces:


          int accumulator = 0;
          while (++firstNum < secondNum) {
                if (firstNum % 2 != 0) {
                      System.out.println(firstNum);
                }
                else {
                      accumulator += firstNum;
                }
          }
          System.out.println(accumulator);
Avatar of crystalfish0318
crystalfish0318

ASKER

I input 3 and 17, then the output should be 47
but it shows "70".............
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)