Question: I need help with my java code. Exercise 1: Your main program should look like this with main() calling the two new methods loopSum(int n)

I need help with my java code.

Exercise 1: Your main program should look like this with main() calling the two new methods loopSum(int n) and formulaSum(int n) that you will write:

public class IntSumParameters {

public static void main(String[] args) {

int n = 100;

loopSum(n);

formulaSum(n); }

// Your 2 new methods go here.

}

Both of these new methods, loopSum() and formulaSum() will calculate the sum and print the results. The loopSum() uses a loop and formulaSum() uses the formula. Just move the code to do the calculations into these methods. Add print statements to print the output using the parameter and the calculated result formatted like this:

The sum of the numbers from 1 to 100 is 5050. Calculated with a loop.

The sum of the numbers from 1 to 100 is 5050. Calculated with the formula.

Run and test your modified program and fix any problems. Copy your modified program and the output from the Console into your solutions document.

Below is my code.

public class IntSumParameters {

public static void main(String[] args) {

int n = 100;

// loopSum(n);

// formulaSum(n);

}

public static void loopSum(int n) {

for (int i = 1; i < n+1 ; i=i+1 ) {

int 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" + sum + ". Calculated with a formula. ");

}

}

How can I fix this?

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!