Adding Up Even and Odd Integers

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Yes. Each of the three sums correctly calculated.

Adding Up Even and Odd Integers

flowchart of program

Here is a skeleton for the program. Compare the skeleton to the flowchart at right.

import  java.util.Scanner;

// User enters a value N
// Add up odd integers,  
// even  integers, and all integers 1 to N
//
class AddUpIntegers
{
  public static void main (String[] args ) 
  {
    Scanner scan = new Scanner( System.in );
    int N, sumAll = 0, sumEven = 0, sumOdd = 0;

    System.out.print( "Enter limit value:" );
    N = scan.nextInt();

    int count =  ;
    
    while (   )    
    {
      (more statements will go here later.)

       ;
    }

    System.out.print  ( "Sum of all : " + sumAll  );
    System.out.print  ( "/tSum of even: " + sumEven );
    System.out.println( "/tSum of odd : " + sumOdd  );
  }
}

First, we should get the counting loop correct. The loop should count from one up to (and including) the limit. It is often a good idea to write a program in stages (just as a house is built in stages). First, get the foundation correct. The foundation of this program is the counting loop.

QUESTION 4:

Fill in the three blanks.