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
Please help!! thank you so much!! &lt;3
Zepler [3.9K]

Answer:

D. 4x as much

Explanation:

The answer is pretty simple. The question is asking about 10m/s and 20m/s right? Take a look at the energy at each point on the graph. At 10m/s the kinetic energy is 100 on the graph. At 20m/s the kinetic energy is 400. You'll notice that 100 x 4 is 400 - That's the answer.

You can also prove this mathematically. The formula for kinetic energy is

\frac{1}{2}mv^2

The question says: constant mass so we can ignore that for now.

(1/2) * 10^2 is (1/2) * 100 = 50

for the 20m/s ball it would be

(1/2) * 20^2 = 400 * 1/2 = 200

Once again, you see that it is 4x

3 0
2 years ago
Read 2 more answers
(a) Store last 7 digits of your student ID in a vector (7 element row or column vector). Write a MATLAB code which creates a 7x7
astra-53 [7]

Answer:

MATLAB code explained below with appropriate comments for better understanding

Explanation:

clc

clear all

ID = [1 2 3 4 5 6 7]; % Replace this with your student ID

%(a)

A = zeros(7);

for i=1:7

 

A(i,i)= ID(i);

 

end

fprintf('A =\n');

disp(A);

B = diag(ID);

fprintf('B =\n');

disp(B);

fprintf('Both A and B are same\n');

%(b)

if(mod(A(6,6),2)==0)

fprintf('A(6,6) is even\n');

else

fprintf('A(6,6) is odd\n');

end

%(c)

if(A(3,3)>0)

fprintf('A(3,3) is positive\n');

else if(A(3,3)<0)

fprintf('A(3,3) is negative\n');

else

fprintf('A(3,3)=0\n');

end

end

%(d)

fprintf('\nRequired series : ');

n = 35;

while n>=0

fprintf('%i',n);

if(n>0)

fprintf(', ');

end

n = n - 5;

end

fprintf('\n');

%(e)

n = input('\nInput an integer : ');

fprintf('%i! = ',n);

F = 1;

while n>1

F = F * n;

n = n - 1;

end

fprintf('%i\n',F);

%(f)

clear all

x = [3 7 2 1];

y = [4 3 9 1];

A = 0;

C = x(1);

for i=1:length(x)

A = A + x(i)*y(i);

B(i) = x(i)/y(i);

if(min(x(i),y(i))<C)

C = min(x(i),y(i));

end

end

C = 1/C;

fprintf('\nA = %i\n',A);

fprintf('B = ');

disp(B)

fprintf('C = %0.5g\n',C);

%(g)

clear all

A = randi([5 25],[1,10]);

maxA = A(1);

for i = 2:10

if(maxA<A(i))

maxA = A(i);

end

end

minA = A(1);

i = 2;

while i<11

if(minA>A(i))

minA = A(i);

end

i = i+1;

end

fprintf('\nA = ');

disp(A);

fprintf('maxA = %i\n',maxA);

fprintf('minA = %i\n',minA);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%output

A =

1 0 0 0 0 0 0

0 2 0 0 0 0 0

0 0 3 0 0 0 0

0 0 0 4 0 0 0

0 0 0 0 5 0 0

0 0 0 0 0 6 0

0 0 0 0 0 0 7

B =

1 0 0 0 0 0 0

0 2 0 0 0 0 0

0 0 3 0 0 0 0

0 0 0 4 0 0 0

0 0 0 0 5 0 0

0 0 0 0 0 6 0

0 0 0 0 0 0 7

Both A and B are same

A(6,6) is even

A(3,3) is positive

Required series : 35, 30, 25, 20, 15, 10, 5, 0

Input an integer : 6

6! = 720

A = 52

B = 0.7500 2.3333 0.2222 1.0000

C = 1

A = 25 14 7 10 13 17 10 17 19 9

maxA = 25

minA = 7

>>

5 0
2 years ago
Why doesn't the ad load ?
baherus [9]
Try refreshing the page, if not restart your device check your internet etc.
3 0
2 years ago
Read 2 more answers
Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person ha
Lera25 [3.4K]

Answer:

The definition of function is as follows:

def typing_speed(number_of_words,Time_Interval):

number_of_words>=0  

Time_Interval>0

speed=float(60*number_of_words/Time_Interval)

return speed

Explanation:

Above function is defined step-by-step as follows:

        def typing_speed(number_of_words,Time_Interval):

  • A function named typing speed has two arguments, num_of_words and Time_Interval.

                             number_of_words>=0  

                             Time_Interval>0

  • The variable number_of_words is the number of words entered that a person enters, they must be greater than or equal to 0. Where as Time_Interval is the variable for counting the time span in seconds, it must be greater than 0.

                    speed=float(60*number_of_words/Time_Interval)

                    return speed

  • For determining result firstly the seconds are converted int minutes by multiplying with 60 and number_of_words is divided with Time_Interval in order to get words per minute. The return value will give speed which has data type float.

4 0
2 years ago
Write down the numbering system from base 2 to 200​
natka813 [3]

Answer:

So, 11001000 is the binary equivalent of decimal number 200 (Answer).

 

3 0
3 years ago
Other questions:
  • Microprocessors can’t directly understand programming languages, so programs have to be converted into _____________ that corres
    15·1 answer
  • Which of the following statements represents the number of columns in a regular two-dimensional array named values?
    9·1 answer
  • What is the difference between an internal and an external method call? In what situation would only internal calls be needed?
    11·1 answer
  • A digital certificate system: Group of answer choices uses digital signatures to validate a user's identity. is used primarily b
    13·1 answer
  • Identify three best -selling tablet on the market, and decide on the one that you would like to buy? justify your respone
    9·1 answer
  • Describe the basic features of the relational data model and discuss their importance to the end user and the designer. Describe
    9·1 answer
  • Services such as water, electricity, and phone communications are called:
    7·1 answer
  • JPG is considered a lossy file format. What does this mean?
    15·2 answers
  • What were the important developments that occurred in photography that facilitated the creation of motion pictures? Two critical
    6·1 answer
  • if you want to clear a single file, what move instruction would you use? group of answer choices none of the choices file to wor
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!