Solved

use while loop to output the sum of all even numbers

Posted on 2003-10-27
5
768 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

919 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now