frankthetank5345 frankthetank5345
  • 23-01-2020
  • Computers and Technology
contestada

What is the output of the following program?
int sum = 0; int k = 1; int val = 1; for (k = 0; k <= 8; k++) { sum += val; val++; }
System.out.println(sum);

Respuesta :

CPED
CPED CPED
  • 23-01-2020

Answer:

Output of the program is following

sum=45

Explanation:

The execution of the program in the "for" loop  will be following

For k=0

=> val=1 and sum=0+1=1

For k=1

=> val=2 and sum=1+2=3

For k=2

=> val=3 and sum=3+3=6

For k=3

=> val=4 and sum=6+4=10

For k=4

=> val 5 and sum=10+5=15

For k=5

=> val=6 and sum=15+6=21

For k=6

=> val =7 and sum=21+7=28

For k=7

=> val=8 and sum=28+8=36

For k=8

val=9 and sum=36+9=45

So all the iterations of for loop are completed and the output is 45.

I hope it will help you!

Answer Link

Otras preguntas

Please write a paragraph about the Cold War. If you do, 100 points. NO CHEATING
GOOD ANSWER PLSSSS...​
why does the bot kadie keep deleting my questions
Which equation or function is linear?
please. i am hanging on by a thread.
Investment X offers to pay you $6,900 per year for 9 years, whereas Investment Y offers to pay you $9,300 per year for 5 years. a. If the discount rate is 7 per
if you are to measure the following figure for real,what tool(ruler or meter stick) and unit of measur(m, cm, in,) will you use..........help me guys plsss​
Bananas Foster is an example of a dessert that is flambéed. A Bananas Foster label states the accepted number of Calories to be only 300 calories, but a calorim
4. Why was John Q made the doctor?
Which of these terms best describes the size of the Lilliputians? diminutive prodigious colossal gigantic