Java program that uses a loop to compute the sum of all even numbers between 2 and 100 (inclusive)
Posted by Samath
Last Updated: April 03, 2022

Write a Java program that uses a loop to compute the sum of all even numbers between 2 and 100 (inclusive).

Code:

import java.util.Scanner;

public class Main
{
	public static void main(String[] args) {
	    
	   double total = 0;
	    for (int i = 0; i < 101; i += 2) {
	        total += i;
	    }
	   System.out.println("Result: " + total);
	}
}
Related Content
Sum of two values in C
Sum of two values in C
Samath | Nov 09, 2014