Advertisement
Guest User

OddSum 1.2

a guest
Feb 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 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.                 if ((i + 2)<= max)
  26.                 System.out.printf("%s + " , i);
  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.                
  35.                 System.out.printf("%s + ", i);
  36.                
  37.             }
  38.  
  39.         }  
  40.         else {
  41.             for (int i = 1; i <= max; i +=2) {
  42.                 total = total + i;
  43.                
  44.                 System.out.printf("%s + " , i);
  45.                
  46.             }
  47.             System.out.println("= " + total);  
  48.             System.out.print(total + " = ");
  49.  
  50.             for(int i = max; i >= 1; i-=2) {
  51.                 total = total - i;
  52.                
  53.                 System.out.printf("%s + ", i);
  54.                
  55.  
  56.             }
  57.  
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement