/* Ch5Ex9x.java CIS 160 David Klick 2004-10-22 Chapter 5, exercise 9 done more concisely. */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Ch5Ex9x { public static void main(String[] args) throws IOException { BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); int n1, n2, tmp, i, sum, start; // 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 start = n1; if (start % 2 == 0) start++; for (i=start; i<=n2; i+=2) System.out.print(i + " "); System.out.println(); // output sum of even numbers between n1 and n2 start = n1; if (start % 2 != 0) start++; for (i=start, sum=0; i<=n2; i+=2) sum += i; System.out.println("The sum of the even numbers is " + sum); // output squares from 1 to 10 System.out.println("n\tn * n"); for (i=1; i<=10; i++) System.out.println(i + "\t" + (i*i)); System.out.println(); // output sum of squares of odd numbers from n1 to n2 start = n1; if (start % 2 == 0) start++; for (i=start, sum=0; i<=n2; i+=2) sum += (i * i); System.out.println("The sum of the odd squares is " + sum); // output all uppercase letters for (char c='A'; c<='Z'; c++) System.out.print(c); } }