Вот программа на Java, позволяющая найти сумму всех четных чисел от 1 до 10:
public class SumOfEvenNumbers {
public static void main(String[] args) {
int sum = 0;
// Method 1: Using a for loop
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println("Method 1 - Sum of even numbers: " + sum);
// Method 2: Using a while loop
sum = 0;
int num = 1;
while (num <= 10) {
if (num % 2 == 0) {
sum += num;
}
num++;
}
System.out.println("Method 2 - Sum of even numbers: " + sum);
// Method 3: Using a do-while loop
sum = 0;
num = 1;
do {
if (num % 2 == 0) {
sum += num;
}
num++;
} while (num <= 10);
System.out.println("Method 3 - Sum of even numbers: " + sum);
}
}