Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+What is the nature of the plant or site-level role of unions?

Answered: 1 week ago