Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package By3andBy5;
- import java.util.ArrayList;
- public class By3andBy5 {
- public static void main(String[] args) {
- final int MAX_VAL = 1000;
- ArrayList<Integer> byThree = new ArrayList<>();
- ArrayList<Integer> byFive = new ArrayList<>();
- int sum = 0;
- for(int i = 1; i < MAX_VAL; i++){
- if (i % 3 == 0){
- boolean add3 = byThree.add(new Integer(i));
- }else if (i % 5 == 0){
- boolean add5 = byFive.add(new Integer(i));
- }
- }
- System.out.println("Counting by 3's:");
- System.out.println();
- for(int by3 : byThree){
- sum = sum + by3;
- System.out.print(by3 + " ");
- }
- System.out.println();
- System.out.println();
- System.out.println("Counting by 5's:");
- System.out.println();
- for(int by5 : byFive){
- sum = sum + by5;
- System.out.print(by5 + " ");
- }
- System.out.println();
- System.out.println();
- System.out.println("If you add the elements of the first array"
- + " with the elements from second array, \n"
- + "the sum of their respective values is equal to: "
- + sum);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement