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
Whats the next lyric,i baked you a pie,pie,pie
eduard
“oh boy what flavor”
4 0
2 years ago
Read 2 more answers
Explain the difference between true north and magnetic north.
Furkat [3]
Magnetic north is the north a compass points in the direction of. True north, on the other hand, is north based on the earth's axis.
4 0
3 years ago
What do you setup when you want to filter traffic from getting through a firewall?
Andreyy89

Explanation:

Windows Firewall

Windows Firewall is a packet filter and stateful host-based firewall that allows or blocks network traffic according to the configuration. A packet filter protects the computer by using an access control list (ACL), which specifies which packets are allowed through the firewall based on IP address and protocol (specifically the port number). A stateful firewall monitors the state of active connections and uses the information gained to determine which network packets are allowed through the firewall. Typically, if the user starts communicating with an outside computer, it remembers the conversation and allows the appropriate packets back in. If an outside computer tries to start communicating with a computer protected by a stateful firewall, those packets are dropped automatically unless access was granted by the ACL.

6 0
2 years ago
How do I change the year I was born on this website? I made a mistake and now I can't fix it. Can somebody please help me?
Eddi Din [679]

Answer:

you can go to settings and try to fix it or contact the people of this website

Explanation:

8 0
3 years ago
Spreadsheet feature that can be used to arrange data from highest to lowest based on average<br>​
Juliette [100K]

Answer:

RANK.AVG

Explanation:

Required

Arrange data in descending order based on average

The feature to do this is to use the RANK.AVG() function.

By default, the function will return the ranks of the selected data in descending order (i.e. from highest to lowest); though, the sort order can be changed to ascending order.

The syntax is:

=RANK.AVG (number, ref, [order])

Where

number \to The number to use as rank

ref \to The cell range

order \to 0 represents descending order while 1 represents ascending order

8 0
3 years ago
Other questions:
  • A network administrator is implementing dhcpv6 for the company. the administrator configures a router to send ra messages with m
    6·1 answer
  • 9. The best way to insert an existing Excel chart into a document is to A. use the Object command. B. click the Insert tab and c
    6·2 answers
  • Windows server 2012 r2 includes hyper-v in which edition(s)?
    12·1 answer
  • How to write a self-analysis essay
    5·1 answer
  • Frank develops a questionnaire for his study on Internet dating. One of his questions asks, "How do you feel about Internet dati
    14·2 answers
  • Plis is a unit test !!!!!!! HelP ASAP only 30 mins left, will mark as brainliest
    8·1 answer
  • "A user reports that the corporate web server cannot be accessed. A technician verifies that the web server can be accessed by i
    8·1 answer
  • We will use linear interpolation in a C program to estimate the population of NJ between the years of the census, which takes pl
    10·1 answer
  • Please helpppppppppppppp
    11·1 answer
  • What is the difference between the wiring configurations for a residential, 8-position, 8-contact (8P8C) modular plug and jack i
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!