?
Solved

use while loop to output the sum of all even numbers

Posted on 2003-10-27
5
Medium Priority
?
783 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:crystalfish0318
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9626011
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);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9626021
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);
0
 

Author Comment

by:crystalfish0318
ID: 9626109
I input 3 and 17, then the output should be 47
but it shows "70".............
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 9626150
These are the even numbers between 3 and 17. Their sum is seventy:

3
------------ 4
5
------------ 6
7
------------ 8
9
------------ 10
11
------------ 12
13
------------ 14
15
------------ 16
17
------------  +
------------ 70
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9626184
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question