Advertisement
tolfasn

By3andBy5

Nov 9th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. package By3andBy5;
  2. import java.util.ArrayList;
  3. public class By3andBy5 {
  4.     public static void main(String[] args) {
  5.    
  6.         final int MAX_VAL = 1000;
  7.         ArrayList<Integer> byThree = new ArrayList<>();
  8.         ArrayList<Integer> byFive = new ArrayList<>();
  9.         int sum = 0;
  10.        
  11.         for(int i = 1; i < MAX_VAL; i++){
  12.             if (i % 3 == 0){
  13.                 boolean add3 = byThree.add(new Integer(i));
  14.         }else if (i % 5 == 0){
  15.                 boolean add5 = byFive.add(new Integer(i));
  16.         }
  17.         }
  18.        
  19.         System.out.println("Counting by 3's:");
  20.         System.out.println();
  21.         for(int by3 : byThree){
  22.           sum = sum + by3;  
  23.          System.out.print(by3 + " ");
  24.         }
  25.         System.out.println();
  26.         System.out.println();
  27.        
  28.         System.out.println("Counting by 5's:");
  29.         System.out.println();
  30.         for(int by5 : byFive){
  31.           sum = sum + by5;  
  32.          System.out.print(by5 + " ");
  33.         }
  34.        
  35.         System.out.println();
  36.         System.out.println();
  37.         System.out.println("If you add the elements of the first array"
  38.                             + " with the elements from second array, \n"
  39.                             + "the sum of their respective values is equal to: "
  40.                             + sum);
  41.         System.out.println();
  42.     }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement