Given two integers low and high representing a range, return the sum of the integers in that range. For example, if low is 12 and high is 18, the returned values should be the sum of 12, 13, 14, 15, 16, 17, and 18, which is 105. If low is greater than high, return 0.

Answers

Answer 1

You get the 0 if low is greater than high for free, as the loop never runs any iterations if this is the case. The initial value of 0 remains and is returned.

Code for the given question:

public class RangeTest1 {

   public static void main(String[] args){

       System.out.println(sumRange(12, 18)); // prints 105

       System.out.println(sumRange(18, 12)); // prints 0

       System.out.println(sumRange(18, 18)); // prints 18

   }

   public static int sumRange(int low, int high)

   {

       int sum = 0;

       for (int val = low; val <= high; val++){

           sum += val;

       }

      return sum;

   }

}

Or you could use math, although you have to be careful of overflow if high is very large:

public static int sumRange(int low, int high)

{

 return high >= low ? (high*(high+1) - low*(low-1))/2 : 0;

}

To know more about Loops, visit: https://brainly.com/question/16922594

#SPJ4


Related Questions

similQuestion 2
I want to create an Excel workbook to keep track of my spending by month for the year, what would be the best names to give my worksheets?

1 point

Budget1, Budget2, Budget3


Spend1, Spend2, Spend3


June, July, August


Sheet1, Sheet2, Sheet3ar meaning in stability

Answers

The best names to give the worksheets for spending by month for the year will be June, July, August. The correct option is C.

What is a workbook?

A worksheet is a single page in a file created with an electronic spreadsheet tool such as Microsoft Excel or Sheets. A workbook is an Excel file that contains one or more worksheets.

A workbook is an Excel file that, like pages in a book, contains different sheets called worksheets.

To save a workbook, it must have at least one sheet. Excel is a set of work sheets (Sheet 1, Sheet 2, etc) Workbook is a collection of worksheets.

Because they are planning monthly spending, it is best to keep the file name as month name for convenience.

Thus, the correct option is C.

For more details regarding excel, visit:

https://brainly.com/question/3441128

#SPJ1

What is computer hardware tools?

Answers

Answer: Well, in basic terms, they are what makes a computer work. Such as the CPU, GPU, Graphics Card, Motherboard, SSD, and HHD.

Explanation:

Which of the following statements about computational thinking are true? Select 3 options.
Responses

Computational thinking is a set of techniques used to help solve complex problems or understand complex systems.
Computational thinking is a set of techniques used to help solve complex problems or understand complex systems.

The result of computational thinking is a problem broken down into its parts to create a generalized model and possible solutions.
The result of computational thinking is a problem broken down into its parts to create a generalized model and possible solutions.

Computational thinking is basically synonymous with programming.
Computational thinking is basically synonymous with programming.

Computational thinking involves the techniques of decomposition, pattern recognition, abstraction, and algorithm development.
Computational thinking involves the techniques of decomposition, pattern recognition, abstraction, and algorithm development.

Understanding all of the minor details about a problem is critical in computational thinking.

Answers

The  statements about computational thinking that are true are:

1.The result of computational thinking is a problem broken down into its parts to create a generalized model and possible solutions.  

2. Computational thinking involves the techniques of decomposition, pattern recognition, abstraction, and algorithm development.

4. Computational thinking is a set of techniques used to help solve complex problems or understand complex systems.

What does computational thinking primarily aim to achieve?

Computational thinking (CT) is the ability to use ideas, approaches, methods, problem-solving techniques, and logical reasoning that are drawn from computer science and computing to solve issues in a variety of contexts, including those that arise in daily life.

In order to fully participate in a computational world, one must have a set of interrelated skills and practices for solving complex problems. These skills and practices can be learned across many different disciplines.

Learn more about computational thinking from

https://brainly.com/question/19189179
#SPJ1

In microsoft access, when you have the field name firstname display as first name, you are changing what field property?.

Answers

In microsoft access, when you have the field name firstname display as first name, you are changing caption property of the field as If you don't specify a caption for a table field, the field's FieldName property setting will be used as the caption of a label attached to a control or as the column heading in Datasheet view.

Caption property in MIcrosoft Access:

A string expression that can hold up to 2,048 characters is the Caption property.

A table field's FieldName property setting will be used as the caption of a label attached to a control or as the column heading in Datasheet view if you don't specify a caption for the field. T

he caption for the underlying table field will be used if you don't specify one for a query field. A form, button, or label won't have a caption if you don't set one, so Microsoft Access will give it a name based on the object, like Form1.

The FieldName property setting for the field will be copied to the control's Name property box and will also appear in the label of the control created if you create a control by dragging a field from the field list without specifying a Caption property setting for the field.

When the HyperlinkAddress or HyperlinkSubAddress property is set for a label or command button control, the text of the Caption property serves as the hyperlink display text.

To know more about Microsoft Access, visit:https://brainly.com/question/24643423

#SPJ4

what is a translator​

Answers

translates from one language into another, especially as a profession.

Why would a programmer want to use an array instead of simple variables?

OA. To reduce the number of blocks used in a program
O B. To write code that is more interactive
OC. To include more detail and complexity
O D. To use a wider variety of code objects

Answers

To use a wider variety of code objects a programmer wants to use an array instead of simple variables

What is an array?

An array is a collection of elements of the same type that are stored in contiguous memory locations and can be individually referenced by using an index to a unique identifier. Five int values can be declared as an array without having to declare five different variables.

One of the most significant advantages of arrays is that they can be declared once and reused multiple times. It represents multiple values using a single variable. This improves code reusability and readability.

An array's memory can be allocated dynamically. This advantage of an array aids in the saving of system memory. It is also useful when the pre-defined array does not have enough memory. Memory can be manually allocated during run time. Furthermore, when memory allocation is not dynamic, data is stored in contiguous memory locations. The amount of storage required is determined by the type or size of the data.

Hence to conclude  a wider variety of code objects are used

To know more on arrays follow this link

https://brainly.com/question/28061186

#SPJ1

Other Questions
In Quadrilateral ABCD , ABCD and m2=31. What is m5? Enter your answer in the box. Trapezoid A B C D with segment B D. Angle A D B is angle 1. Angle B D C is angle 2. Angle C is angle 3. Angle A is angle 4. Angle A B D is angle 5. Angle C B D is angle 6. yamin operates a small business with a few part-time employees. her annual employment tax withholding in 2021 is $467.80. yamin should file which employment tax reporting form? the following statements concerning petroleum are all true except: a. some of the commercial uses of petroleum fractions include gasoline and kerosene. b. it is a thick, dark liquid composed mostly of hydrocarbons. c. all of its hydrocarbon chains contain the same number of carbon atoms. d. it was probably formed from the remains of ancient marine organisms. e. it must be separated into fractions (by boiling) in order to be used efficiently. please help, ill mark brainlyest Identify this instrument.A. MicroscopeB. Refracting telescopeC. Reflecting telescopeD. Radio Telescope list 2 accomplishments for King David hi! could i get some help on this pls! ty the tell tale heart: What details show the contrast between the narrators perception of the noise and the police officers? What conclusions can you draw about this noise? Which is the prominent c-shaped fiber tract that connects the right and left cerebral hemispheres?. URGENT!! ILL GIVEBRAINLIEST!!!! AND 100 POINTS!!! Which list of minerals is grouped based on color?bornite, chalcopyrite, and hematitechalcopyrite, pyrrhotite, and pyritehematite, bornite, and pyritepyrrhotite, goethite, and hematite In paragraph 9 the author argues against the idea that recent advances in technology have so far offered the majority of workers better pay or positions. What evidence does she provide in support of her view Mexico city has beautiful mountains nearby,but why are they a problem for those who live in the city? Which literary term can be defined as "a guess about events that a reader makes, based on evidence from the text."predictionsatirehyperbolerealism Did the Industrial Revolution require the government to be a democracy? Find the value of X. The diagram is not to scale. how can children and adolescents increase muscle strength during training without significantly increasing muscle mass? What does the Social Security tax provide money for?(Select the best answer.)O Medical benefits for people ages 65 and overO UnemploymentO Income for retired and disabled people and their familiesO FUTA? A study of the population of deer in a park finds that the population decreases by 1.2% each year. The deer population at the beginning of the study is 525. Write an equation to model the population of deer in the park after x years. After how many years will the population of deer in the park be less than 200? describe the events that occur from the time a motor neuron releases acetylcholine at the myoneural junction until muscle cell contraction occurs