/* Ch5Ex8.java CIS 160 David Klick 2004-10-22 Various while loop problems (answer to Chapter 5, exercise 8) */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Ch5Ex8 { public static void main(String[] args) throws IOException { BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); int n1, n2, tmp, i, sum; // get two integers from user System.out.print("Please enter an integer: "); n1 = Integer.parseInt(kbd.readLine()); System.out.print("Please enter an integer larger than " + n1 + ": "); n2 = Integer.parseInt(kbd.readLine()); // guarantee than n1 is less than or equal to n2 if (n1 > n2) { tmp = n1; n1 = n2; n2 = tmp; } // output all odd numbers between n1 and n2 i = n1; if (i % 2 == 0) i++; while (i<=n2) { System.out.print(i + " "); i+=2; } System.out.println(); // output sum of even numbers between n1 and n2 i = n1; if (i % 2 != 0) i++; sum = 0; while (i<=n2) { sum += i; i+=2; } System.out.println("The sum of the even numbers is " + sum); // output squares from 1 to 10 System.out.println("n\tn * n"); i = 1; while (i<=10) { System.out.println(i + "\t" + (i*i)); i++; } System.out.println(); // output sum of squares of odd numbers from n1 to n2 i = n1; if (i % 2 == 0) i++; sum = 0; while (i<=n2) { sum += (i * i); i+=2; } System.out.println("The sum of the odd squares is " + sum); // output all uppercase letters char c = 'A'; while (c <= 'Z') { System.out.print(c); c++; } } }