Core Java

Calculating the Sum of First N Even Numbers Divisible by 3 in Java

In this article, we’ll explore how to calculate the sum of the first N even numbers that are divisible by 3 in Java. We will start by providing a solution using a simple for loop, and then we’ll demonstrate an alternative solution using Java Streams.

1. Problem Definition

Given a number N, we need to find the sum of the first N even numbers that are divisible by 3. For example, if N = 7, the first seven even numbers are 0, 2, 4, 6, 8, 10, 12. From these, only the numbers 0, 6, 12 are divisible by 3, so the sum would be 0 + 6 + 12 = 18.

2. Using a for Loop

We can iterate through the first N even numbers, check if each one is divisible by 3, and then add it to the sum if it is. Here’s how we can implement this solution:

public class SumOfEvenNumbersDivisibleby3 {

    public static void main(String[] args) {
        int NUMBER = 7;
        int result = sumOfEvenNumbersDivisibleBy3(NUMBER);
        System.out.println("The sum of the first " + NUMBER + " even numbers divisible by 3 is: " + result);
    }

    public static int sumOfEvenNumbersDivisibleBy3(int N) {
        int sum = 0;
        
        for (int i = 0; i < N; i++) {
            int num = i * 2;
            
            if (num % 3 == 0) {
                sum += num;
            }
        }
        
        return sum;
    }
}

In the above code example, the sumOfEvenNumbersDivisibleBy3 method begins by initializing a sum variable to 0. A for loop is used to iterate from i = 0 to i = N - 1, generating the even numbers by multiplying i by 2. For each even number, we check if it is divisible by 3 using num % 3 == 0. If the condition is true, the number is added to the sum. After the loop completes, the final sum of the even numbers divisible by 3 is returned.

In the main method, we set the variable NUMBER to 7, indicating that we want to find the sum of the first 7 even numbers divisible by 3. The method sumOfEvenNumbersDivisibleBy3(NUMBER) is then called, and its result is printed to the console.

Sample Output:

For NUMBER = 7, the output will be:

The sum of the first 7 even numbers divisible by 3 is: 18

3. Using Java Streams

The above solution uses a basic loop, but we can also implement this using Java Streams, which provides a more functional approach to processing sequences of data. Here is how we can implement the same solution using Streams:

public class StreamSumEvenNumbersDivisibleBy3 {
    
    public static void main(String[] args) {
        int NUMBER = 7;
        int result = sumOfEvenNumbersDivisibleBy3(NUMBER);
        System.out.println("The sum of the first " + NUMBER + " even numbers divisible by 3 is: " + result);
    }

    public static int sumOfEvenNumbersDivisibleBy3(int N) {
        return IntStream.range(0, N)
                .map(i -> i * 2)
                .filter(num -> num % 3 == 0)
                .sum();
    }
}

In the sumOfEvenNumbersDivisibleBy3 method, we use IntStream.range(0, N) to generate a stream of numbers from 0 to N - 1, then apply map(i -> i * 2) to convert each number into its corresponding even number. The filter(num -> num % 3 == 0) step filters the stream to include only those numbers divisible by 3, and finally, .sum() computes the sum of the filtered numbers.

Sample Output:

For NUMBER = 7, the output will be the same as the previous solution:

The sum of the first 7 even numbers divisible by 3 is: 18

For NUMBER = 10, the first 10 even numbers are 0, 2, 4, 6, 8, 10, 12, 14, 16, 18. Among these, the numbers divisible by 3 are 0, 6, 12, 18. The sum of these numbers is 0 + 6 + 12 + 18 = 36.

Thus, the sample output for NUMBER = 10 would be:

Example output screenshot: Java sum of the first N even numbers divisible by three

4. Conclusion

In this article, we explored two approaches to calculating the sum of the first N even numbers divisible by 3 in Java. The first solution used a traditional for loop to iterate through the even numbers, check divisibility by 3, and sum the qualifying numbers. The second solution leveraged the power of Java Streams, providing a more concise and functional approach to achieve the same result.

5. Download the Source Code

This article explored how to calculate the sum of the first N even numbers that are divisible by three in Java.

Download
You can download the full source code of this example here: Java sum first n even numbers divisible by three

Omozegie Aziegbe

Omos Aziegbe is a technical writer and web/application developer with a BSc in Computer Science and Software Engineering from the University of Bedfordshire. Specializing in Java enterprise applications with the Jakarta EE framework, Omos also works with HTML5, CSS, and JavaScript for web development. As a freelance web developer, Omos combines technical expertise with research and writing on topics such as software engineering, programming, web application development, computer science, and technology.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button