Question: I need help with java. I want to change this code to return a value and no longer contain a println(). public class IntSumParameters {
I need help with java. I want to change this code to return a value and no longer contain a println().
public class IntSumParameters {
public static void main(String[] args) {
int n = 100;
loopSum(n);
formulaSum(n);
}
public static void loopSum(int n) {
int sum = 0;
for (int i = 1; i < n+1 ; i++ ) {
sum = sum + i;
}
System.out.println("The sum of the numbers from 1 to 100 is " + sum + ". Calculated with a loop.");
}
public static void formulaSum(int n) {
int formulaSum = n*(n+1)/2;
System.out.println("The sum of the numbers from 1 to 100 is " + formulaSum + ". Calculated with the formula.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
