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
describe the relationship between the policy at your organization and the inclusion of additional data streams into electronic h
katen-ka-za [31]

The relationship between the policies at your organization and additional data streams into electronic health records is the policies give the organization a direction towards electronic data.

<h3>What are policies?</h3>

Policies are the rules and regulations of a place. The policies are the backbone of an organization. It creates an identity of the organization. Due to digitalization, all policies and rules are written electronically.

Thus, your organization's policies and additional data streams into electronic health records are related in that the policies point the organization in the direction of electronic data.

To learn more about policies, refer to the link:

brainly.com/question/15586599

#SPJ4

7 0
2 years ago
Jenn wants to assign a value to the favorite car variable: favoriteCar = Toyota but gets an error message. What does she need to
Valentin [98]

Answer:

The correct answer is:

Option D: Put Quotation marks around the string

Explanation:

There are various data types that are used to store data in programming i.e. numeric, alphabetic, alphanumeric etc.

Given that Jenn wants to store the value in variable. The names of cars are usually rods and the data type for them is string.

Whenever a string is used, quotation marks are used around it. The quotation marks make it easier for the compilers and interpreters to identify string.

Hence,

The correct answer is:

Option D: Put Quotation marks around the string

8 0
3 years ago
How do you make someone the Brainliest?
yaroslaw [1]
You click brainliest
8 0
2 years ago
4.2 Code Practice: Question 1<br> Can someone help me write the code in python language?
alekssr [168]

Answer:

number = int(input("Enter a number: "))

sum = number

c = 1

while (sum<=100):

   number = int(input("Enter a number: "))

   sum = sum + number

   c = c + 1

print("Sum: " + str(sum))

print("Numbers Entered: " + str(c))

Explanation:

Hope this helps lovely :)

8 0
3 years ago
Can someone can help me am dont know how much RAM do i need. I use my pc for work and to watch yt vid. Thanks​
Vitek1552 [10]

Answer:

at least 2gb

Explanation:

6 0
3 years ago
Other questions:
  • Which of the following is a full selector?
    12·1 answer
  • We can harden a host by: a. Limiting physical access to it b. Turning off unnecessary services c. Installing patches d. All of t
    8·1 answer
  • What were the advantages and disadvantages of agricultural technology to us.
    12·2 answers
  • How much health did a supply drop balloon originally have??
    7·2 answers
  • A specially formatted encrypted message that validates the information the CA requires to issue a digital certificate is known a
    6·1 answer
  • ON QUIZ PLEASE HELP
    13·1 answer
  • What do you call the spreadsheet cell that is in effect and has a heavier black border around it?
    7·2 answers
  • . Consider the problem of finding the largest element in a list of n elements. What will be the basic operation of an algorithm
    9·1 answer
  • When a virtual machine is
    9·1 answer
  • Which of these would be the fastest transition duration?
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!