You can do it by using a loop and from what I understand, a seperate method, which we’ll call addSum
. In addSum, we will create a for loop with the starting limits and the ending limit.
public static void addSum(int start, int end) {
int addMe = 0;
for(; start <= end; start++) {
addMe += start;
}
System.out.println("The result: " + addMe);
}
And the main method looks like this:
public static void main(String[] args) {
addSum(2, 4); // Replace this with the numbers you want
}
So the whole code looks like this (assuming your file is called Main.java):
public class Main {
public static void main(String[] args) {
addSum(2, 4); // Replace this with the numbers you want
}
public static void addSum(int start, int end) {
int addMe = 0;
for(; start <= end; start++) {
addMe += start;
}
}
10
solved How to add the sum of ints n to c [closed]