Project Euler #1: Multiples of 3 and 5

  • + 0 comments

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
        int t = in.nextInt();  // Number of test cases
    
        // Iterate through each test case
        for (int i = 0; i < t; i++) {
            int n = in.nextInt();  // The number for which we want to sum multiples of 3 or 5
    
    
            List<Integer> multiples = new ArrayList<>();
    
    
            for (int j = 1; j < n; j++) {
                if (j % 3 == 0 || j % 5 == 0) {
                    multiples.add(j);  
                }
            }
    
    
            int[] array = new int[multiples.size()];
            for (int j = 0; j < multiples.size(); j++) {
                array[j] = multiples.get(j);
            }
    
    
            int sum = 0;  
            for (int num : array) {
                sum += num;
            }
    
            // Output the sum for the current test case
            System.out.println(sum);
        }
    }
    

    }