To find the sum of a sequence of positive integers, you can use a loop to iterate over the integers and add them up. For example, here is a Java program that computes the sum of the first 10 positive integers:
public class Sum { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 10; i++) { sum += i; } System.out.println("The sum of the first 10 positive integers is: " + sum); } }
This program defines a loop that starts at 1 and ends at 10, incrementing by 1 on each iteration. It adds each number to the sum
variable, and then prints the final result after the loop has completed.
If you want to find the sum of a different sequence of integers, you can modify the loop bounds and the increment value to suit your needs. You can also use a different looping construct, such as a while
loop or a do-while
loop, if you prefer.