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
A company's ____________ is the percentage of the total target market for the product that belongs to the company.
grigory [225]

a companys Market share is the percentage of the total target market for the product that belongs to the company

3 0
3 years ago
What is the most happy job work? that can earn $1000 per day at homes? show company website links
tamaranim1 [39]
Https://moneypantry.com/making-1000-dollars-daily/
3 0
3 years ago
Two girls find an MP3 player at the park. They try for three weeks to find the owner, but cannot. Now, Cynthia and Adrianne are
Vikki [24]
Just think who found the phone first 
5 0
3 years ago
Read 2 more answers
When can designers use rapid application development? HELP ASAP
Bess [88]

I pretty sure you it’s the third one c.

6 0
3 years ago
When a linke chain contain nodes that reference both the next node and the previous node it is called a(n)
ch4aika [34]
A. Two way linked chain
4 0
3 years ago
Other questions:
  • Sequentially prenumbered forms are an example of a(n): a. Processing control. b. Data transmission control. c. Input control. d.
    10·1 answer
  • Which of the following Web sites would be MOST credible?
    8·2 answers
  • Which option in presentation software can you use to apply a “fly in” effect to the objects on a slide? A)flowchart B)shapes C)
    11·2 answers
  • Write a Java test program, named TestGuitar, to create 3different Guitars representing each representing a unique test case and
    8·1 answer
  • Given the code segment below, complete the assignments as stated in the comments. struct aaa { int m; int nn; } struct bbb{ stru
    13·1 answer
  • You are installing windows on a new computer. Using the RAID controller on the motherboard, you configure three hard disks in a
    10·1 answer
  • O Why was the Internet originally constructed? Oto enable researchers to communicate
    6·2 answers
  • . What process skill would a scientist use to find the length of a line
    5·1 answer
  • 1. What year was the Entertainment Software Rating Board created?
    15·1 answer
  • JAVA- If you have an int as the actual parameters, will it change to fit the parameters if it requires a double, or will the cod
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!