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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!