Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OddSum {
- /**
- Example output if user enters 10:
- Max?
- 1 + 3 + 5 + 7 + 9 = 25
- 25 = 9 + 7 + 5 + 3 + 1
- Example output if user enters 11:
- Max?
- 1 + 3 + 5 + 7 + 9 + 11 = 36
- 36 = 11 + 9 + 7 + 5 + 3 + 1
- */
- public static void main(String[] args) {
- int max; // user's maximum #
- int total = 0; // total duh!
- TextIO.putln("Max?");
- max = TextIO.getlnInt();
- if (max % 2 == 0) {
- for(int i = 1; i <= max; i+=2) {
- total = total + i;
- System.out.printf("%s + " , i);
- }
- System.out.println("= " + total);
- System.out.print(total + " = ");
- for(int i = max - 1; i >= 1; i-=2) {
- total = total - i;
- System.out.printf("%s + ", i);
- }
- }
- else {
- for (int i = 1; i <= max; i +=2) {
- total = total + i;
- System.out.printf("%s + " , i);
- }
- System.out.println("= " + total);
- System.out.print(total + " = ");
- for(int i = max; i >= 1; i-=2) {
- total = total - i;
- System.out.printf("%s + ", i);
- }
- /*Max?
- 11
- 1 + 3 + 5 + 7 + 9 + 11 + = 36
- 36 = 11 + 9 + 7 + 5 + 3 + 1 + */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement