Given positive integer n, write a for loop that outputs the even numbers from n down to 0. If n is odd, start with the next lower even number.

Answers

Answer 1
Answer:

if(n % 2 == 0){

   for(int i = n; i >= 0; i-=2){

        System.out.println(i);

    }

}

else{

     for(int i = n - 1; i >= 0; i-=2){

        System.out.println(i);

     }

}

Sample output

Output when n = 12

12

10

8

6

4

2

0

Output when n = 21

20

18

16

14

12

10

8

6

4

2

0

Explanation:

The above code is written in Java.

The if block checks if n is even by finding the modulus/remainder of n with 2.  If the remainder is 0, then n is even. If n is even, then the for loop starts at i = n. At each cycle of the loop, the value of i is reduced by 2 and the value is outputted to the console.

If n is odd, then the else block is executed. In this case, the for loop starts at i = n - 1 which is the next lower even number. At each cycle of the loop, the value of i is reduced by 2 and the value is outputted to the console.

Sample outputs for given values of n have been provided above.


Related Questions

When parameters are passed between the calling code and the called function, formal and actual parameters are matched by: a.

Answers

Answer:

their relative positions in the parameter and argument lists.

Explanation:

In Computer programming, a variable can be defined as a placeholder or container for holding a piece of information that can be modified or edited.

Basically, variable stores information which is passed from the location of the method call directly to the method that is called by the program.

For example, they can serve as a model for a function; when used as an input, such as for passing a value to a function and when used as an output, such as for retrieving a value from the same function. Therefore, when you create variables in a function, you can can set the values for their parameters.

A parameter can be defined as a value that must be passed into a function, subroutine or procedure when it is called.

Generally, when parameters are passed between a calling code and the called function, formal and actual parameters are usually matched by their relative positions in the parameter and argument lists.

A formal parameter is simply an identifier declared in a method so as to represent the value that is being passed by a caller into the method.

An actual parameter refers to the actual value that is being passed by a caller into the method i.e the variables or values that are passed while a function is being called.

10. This question refers to the chart from the previous question.
Which of the following conclusions is BEST supported by the scatter plot?
OOOO
A. Most dogs breeds have a maximum weight of 100 lbs or more
B. Most dog breeds have a maximum life span of 10 or fewer years
C. All dog breeds that weigh less than 50 pounds have a maximum lifespan of more than 10 years
D. No dog breeds that weigh more than 150 pounds are expected to live more than 10 years.

Answers

Answer: C. All dog breeds that weigh less than 50 pounds have a maximum lifespan of more than 10 years.

Explanation:

Looking at the scatter plot, it is shown that all dogs that weigh less than 50 pounds have a maximum lifespan that is above 10 years with a number of them even approaching 20 years.

This means that on average, dogs that weigh less tend to live longer than dogs that weigh more which as shown in the scatter plot have a lower lifespan the heavier they are.

Other Questions
find the volume of the irregular figure Read the passage about the UNs Food for Girls' Education Programme.Since 2007, the UN World Food Programme's (WFP) Food For Girls' Education Programme has been tackling hunger and enrollment challenges in Yemen, where more than 60 per cent of primary school-aged children who are not in school are girls. As a result of the programme, families who send their girls to school are eligible to receive an annual ration of wheat and fortified vegetable oil. . . . Since 2010 the programme has reached almost 200,000 girls, benefiting almost 1 million family members.Besides distributing food, what is another role of the UNs Food for Girls Education Programme?. providing funds for building new schools. distributing funds to needy families. encouraging womens education. providing teachers for the girls education . Asif and Ben share $2100 in the ratio Asif: Ben = 3:7.Work out how much Asif receives.$2100 Use Pythagorean theorem to solve the following right triangles, put your answer in simplest radical form: Why do you think some people do not participate in campaigns programs conducted in communities? PLSSSSS HELPPPPP REWARD BRAINLEISTTT 3 feet4 feetinches6 inchesIs InchesWhat is the volume, in cubic feet, of thecement used to make the steps? Show orexplain all of the steps you used to determineyour answer. A person is trying to solve the equation for the energy of a light wave: E=hc . She knows the values of h and c. What does the quantity represent?A.frequencyB.wave speedC.periodD.wavelength The ecosystem with the most concentrated selection of diverse plants and animals in the world is the __________, located in Latin America.A.Atacama DesertB.Amazon RainforestC.coniferous forest in the Central American mountainsD.deciduous forest in the South American mountainsPlease select the best answer from the choices providedABCDthis is this answerhope this help love yall A drum of oil is 4 ft. hieight and the radius is 1.2 ft. Oil cost is $22. What is the cost per cubic ft. to fill the drum of oil?Use 3.14 for pi.Round answer to nearest penny. Three boys were playing balls. Ben's ball bounced eight more times than Menard's . Menard's ball bounced half as many times as Diego's. Diego's ball bounced eight times. How many times did Ben's ball bounced? Can you guys help me For its standard checking account northwest bank charges a fee of 2.50 plus 0.10 per check the Function use to determine total monthly charges is C=2.50+0.10n where C = monthly charges and n= to number of charges If a customer writes 24 check during a month How much would he customer Be charged Shari bought 3 breath mints and received $2.76 change.Jamal bought 5 breath mints and received $1.20 change. If Shari and Jamal had the same amount of money how much does each breath mint cost? please help me with this What word means a conclusion based on too little evidence or on exceptional or biased evidence? I will give brainliest to the correct answer!!!Don't forget...answer both questions Find the circumference of the circle to the nearest hundredth 11cm WILL GIVE BRAINLIEST TO WHOEVER ANSWERS FIRST!!!!! For the compound CHO, what type of bond would join the elements and why?1. covalent because an electron is transferred from a C atom and O atom to a H atom.2. covalent because electrons are shared between the C, H, and O atoms3. ionic because an electron is transferred from a C atom and O atom to a H atom.4. ionic because electrons are shared between the C, H, and O atoms The net of a solid figure is shown below:The net of a solid figure is shown below:Which calculation will give the total surface area of the solid figure?A) 3 x 6 x 6 square inchesB) 6 x 3 x 3 x 3 square inchesC) 3 x 6 x 6 x 6 square inchesD) 6 x 3 x 3 square inches