Advertisement
Guest User

OddSum 1.0

a guest
Feb 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. public class OddSum {
  2.     /**
  3. Example output if user enters 10:
  4. Max?
  5. 1 + 3 + 5 + 7 + 9 = 25
  6. 25 = 9 + 7 + 5 + 3 + 1
  7.  
  8. Example output if user enters 11:
  9. Max?
  10. 1 + 3 + 5 + 7 + 9 + 11 = 36
  11. 36 = 11 + 9 + 7 + 5 + 3 + 1
  12.  
  13.      */
  14.     public static void main(String[] args) {
  15.         int max;    // user's maximum #
  16.         int total = 0; // total duh!
  17.  
  18.         TextIO.putln("Max?");
  19.         max = TextIO.getlnInt();
  20.  
  21.         if (max % 2 == 0) {
  22.  
  23.             for(int i = 1; i <= max; i+=2) {
  24.                 total = total + i;
  25.                 System.out.printf("%s + " , i);
  26.  
  27.  
  28.             }
  29.             System.out.println("= " + total);  
  30.             System.out.print(total + " = ");
  31.  
  32.             for(int i = max - 1; i >= 1; i-=2) {
  33.                 total = total - i;
  34.                 System.out.printf("%s + ", i);
  35.  
  36.             }
  37.  
  38.         }  
  39.         else {
  40.             for (int i = 1; i <= max; i +=2) {
  41.                 total = total + i;
  42.                 System.out.printf("%s + " , i);
  43.  
  44.             }
  45.             System.out.println("= " + total);  
  46.             System.out.print(total + " = ");
  47.  
  48.             for(int i = max; i >= 1; i-=2) {
  49.                 total = total - i;
  50.                 System.out.printf("%s + ", i);
  51.  
  52.  
  53.             }
  54. /*Max?
  55. 11
  56. 1 + 3 + 5 + 7 + 9 + 11 + = 36
  57. 36 = 11 + 9 + 7 + 5 + 3 + 1 +  */
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement