Solved

use while loop to output the sum of all even numbers

Posted on 2003-10-27
5
778 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 20 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
type mismatch (Object[] to double[] 4 41
Bot application - advice 3 63
collection output issue 9 66
passing enum to a method 4 35
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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