1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
OlgaM077 [116]
3 years ago
11

Write a method called sum with a while loop that adds up all numbers between two numbers a and b. The values for a and b can be

passed to the sum method as parameters.
Computers and Technology
2 answers:
svetoff [14.1K]3 years ago
7 0
<h2>Answer:</h2>

  //Method sum header declaration.

   //The return type of the method be of type int,

   //since the sum of integer numbers is an integer.

   //It has the variables a and b as parameter

   public static int sum(int a, int b){

       

       //Since the method will sum numbers between a and b,

       //it implies that a and b are not inclusive.

       //Therefore, increment variable a by 1 before starting the loop

       //and make sure the loop does not get to b

       //by using the condition a < b rather than a <=b

       

       a++;

       

       //Declare and initialize to zero(0) a variable sum to hold the sum of the numbers

       int sum = 0;

       

       //Begin the while loop.

       //At each cycle of the loop, add the value of variable a to sum

       //and increment a by 1

       while(a < b) {        

           sum = sum + a;

           a++;

       }

       

       //at the end of the loop, return sum

       return sum;

       

   }        // End of method sum

   

<h2>Explanation:</h2>

The above code has been written in Java. It also contains comments explaining every section of the code. Please go through the comments for better understanding.

The overall code without comments is given as follows;

  public static int sum(int a, int b) {    

       a++;

       int sum = 0;

       

       while (a < b) {          

           sum = sum + a;

           a++;

      }  

       return sum;

       

   }

lana66690 [7]3 years ago
3 0

Answer:

   public static double sum(int a,int b){

       int sum =0;

       for(int i =a+1; i<b;i++){

         sum = sum+i;

       }

       return sum;

   }

Explanation:

A complete Java program that calls the method sum() is given below;

In creating the method sum; We used a for loop that starts at a+1 and ends at 1 less than b, this is because we want to grab the numbers between them and not including them.

In the body of the for loop, we add each element to the sum variable initially declared and initialized to zero.

public class TestClock {

   public static void main(String[] args) {

   int num1 = 5;

   int num2 = 10;

       System.out.println("The sum of numbers between "+num1+ " and "+num2+ " is "+sum(num1,num2));

   }

   public static double sum(int a,int b){

       int sum =0;

       for(int i =a+1; i<b;i++){

         sum = sum+i;

       }

       return sum;

   }

}

You might be interested in
Algorithm that has been coded into something that can be run by a machine.
hram777 [196]
The answer is program
8 0
3 years ago
If an OS is using paging with offsets needing 12 bits, give the offset (in decimal or hexadecimal) to: the third word on a page
Mariulka [41]

Answer:

Explanation:

If an OS is using paging with offsets needing 12 bits, give the offset (in decimal or hexadecimal) to: the third word on a page the last word on a page.

7 0
3 years ago
)What item is at the front of the list after these statements are executed?
Dominik [7]

Answer:

C.Rudy

Explanation:

A deque waitingLine is created.Then Jack is added to front of the deque first.After that Rudy is added to the front of the deque.AFter that Larry and sam are inserted at the back.So the last item inserted at the front is front of the deque.So Rudy was the last item inserted at the front.

Hence we conclude that the answer is Rudy.

4 0
3 years ago
I want to select between 5 different 2-bit inputs and output it. How many bits do I need to specify which input?
miskamm [114]

Answer:

3 select inputs are required to select a specific input between 5 inputs.

Explanation:

Multiplexer is used to select the inputs from different inputs to a single output. There are many types of multiplexers for different number of inputs selection. 1. 2x1 is the type of multiplexer where 2 inputs and 1 output. This is used to select between these inputs. There is one more pin to select the input between these two inputs that is called <em>select </em>input.

2. Another type is 4x1 multiplexer. There are 4 inputs and one output. In this type of multiplexer 2 select pins are used for selection of input bits between these 4 inputs.

For more than 4 inputs and less than 8 inputs, 8x1 multiplexer is used. this multiplexer uses three inputs for selection purpose.

8 0
3 years ago
Pls help!!!!!!!!!!!!
son4ous [18]

Answer:

i am 100% sure it is (B)

Explanation:

To delete offensive or irrelevant posts  

5 0
3 years ago
Other questions:
  • Describe the Sign in process of an Email account.​
    6·1 answer
  • A very simple device that connects network components and sends packets of data to all other connected devices is called a _____
    5·1 answer
  • What ipconfig command line option can be used to delete cached dns information from memory?​?
    9·1 answer
  • Kaitlin likes a particular design theme, but she is not sure about every single color for bullets, backgrounds, etc found in the
    5·2 answers
  • Claudia has a bachelors degree in computer information systems and she has learned to use some popular software testing tolls wh
    13·2 answers
  • "A ____ is a collection of hardware and other devices that are connected together so that users can share hardware, software, an
    5·1 answer
  • Which type of image is not a supported using the Online Pictures or Insert Picture command?
    5·2 answers
  • If you have two tasks x and y, with y being a successor to x, if we mark x as undone (because it needs to be redone for some rea
    15·1 answer
  • Plz help<br> What is an ASCII?
    10·2 answers
  • An important goal of biosecurity training is to: Prevent laboratory accidents that could expose personnel to hazardous agents. P
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!