v> Write a java programme named AddingNumbers.java that uses a method to sum all
the integers from 10 to 20 but skipping 15 and 16. Invoke the method within the print
command line to print: The sum of all integers from 10 to 20 excluding 15 and 16
is 134.
public class AddingNumbers {
public static int sum() {
int total = 0;
for (int i = 10; i < 21; i++) {
if (i < 15 || i > 16) {
total += i;
}
}
return total;
}
public static void main(String[] args) {
System.out.println("The sum of all integers from 10 to 20 excluding 15 and 16 is " + sum() + ".");
}
}
Comments
Leave a comment