We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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);
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}