Yes. Each of the three sums is calculated to be zero. The flowchart also works for N==1. It is essential to check the logic of a program for simple "boundary cases" like these.
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 0 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 zero 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.
Fill in the three blanks.