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
Discuss some design considerations that you should keep in mind when you want to design a professional-looking flyer or newslett
iogann1982 [59]
Designs on business flyers should be based on the business' theme and the message that it wants to communicate. <span>A sale should have a design that would attract customers with the discounts they can avail with their purchases. </span><span>
</span>Texts, colors, lines, shapes, and images are the elements that should be figured out depending on the kind of communication that a business would want to share. <span>
</span><span>A sale flyer uses big text to emphasize discounts. Advertising products with amounts should require friendly designs with short and clear-cut messages. Colors can also drive customers and associate it with the kind of product. </span>
3 0
3 years ago
I need major help. Word processing
Pie

Answer:

I do Word processing in computing, but I use a computing textbook for studies.

I also don't have any idea about the flashcards.

I'm sorry.

Explanation:

But if you want me to help, I may.

3 0
2 years ago
What type of device does a computer turn to first when attempting to make contact with a host with a known IP address on another
scoray [572]

Answer:

default gateway

Explanation:

A default gateway in a computer network environment means the access point through which a networked device uses to send or receive information from another network. Default gateways make it possible to exchange information from a local network to an outside network. So simply they serve as the interface through which communication is made with external networks or the internet.

6 0
3 years ago
Jennifer stays fit by playing games that track the movement of her body. Which platform features such a physical interface featu
Usimov [2.4K]

Answer:

kinect xbox 360.

Explanation:

for example- just dance

7 0
3 years ago
Why does the position of drawCircle(x, y, r) in the answer choices matter?
Andrei [34K]

Answer:

B and C

Explanation:

xPos and yPos determine the center of the circle, and rad determines the radius of the circle drawn.

It cannot be A because it starts drawing a circle with the center of (4, 1). None of the circles ahve a center at (4, 1). It is B because while it does start at (4, 1), the repeat function adds one to the y and radius. While ti repeats 3 times it ends up drawing all 3 circles. C also works because it starts by drawing the biggest circle and then subtracting the values to make the other two. It cannot be D because in the repeat function it subtracts from the y value and radius too early, it doesn't draw the biggest circle.

6 0
2 years ago
Other questions:
  • _____ is two or more connected computers.
    8·1 answer
  • Sushant is a new manager and he wants to share his ideas and working protocol with his team.Compare the advantages and disadvant
    13·1 answer
  • If you have a list of words that you wish me to make into a bulleted list, you must first highlig or ______ The words with the c
    13·1 answer
  • One of the most famous ___ licenses is gpl, which ensures that a particular program remains free to distribute and allows its co
    10·1 answer
  • The rod and crankshaft convert the up-and-down motion of the piston into
    12·2 answers
  • Which of the following commands would you use to start the program Main with four strings? a. java Main arg0 arg1 arg2 arg3 b. j
    11·1 answer
  • JavaBeans are?A special Java classfileServletsAppletsA Special form of JSPNone of Given
    8·1 answer
  • How do we Rewrite the following Python code to avoid error. mark=inpt("enter your mark ")
    6·1 answer
  • You have two identical print devices that are set up in a work room. Currently, the Windows print server has two printers config
    13·1 answer
  • Which of these technologies has been most used by terrorist organizations?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!