The completed program is given below.
Here is the complete program that adds up even and odd integers from zero the limit, N, specified by the user:
import java.util.Scanner; // User enters a value N // Add up odd integers, // even integers, and all integers 1 to N // public 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 = 1; while ( count <= N ) { sumAll = sumAll + count ; if ( count % 2 == 0 ) sumEven = sumEven + count ; else sumOdd = sumOdd + count ; count = count + 1 ; } System.out.print ( "Sum of all : " + sumAll ); System.out.print ( "\tSum of even: " + sumEven ); System.out.println( "\tSum of odd : " + sumOdd ); } }
It would be odd if you did save this program to a file and run it. An even better idea would be to pretend you had not seen it, and to try to create it from scratch.
Do you need to calculate all three sums sumAll
, sumOdd
, and sumEven
?