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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started