What is the output of the following program with the followi

What is the output of the following program with the following data values entered as inputs ? NOTE: the program will only be run ONE time!

5, 6 , –3 , 7 , –4 , 0, 5 , 8, 9

import java.util.Scanner;

public class Question5

{

public static void main(String[] args)

{

Scanner eek = new Scanner(System.in);

final int LIMIT = 8; int sum, i, number;

boolean done;

sum = 0 ;

i = 1;

done = false;

while ( i <= LIMIT && done==false)

{

// Here is where the user will enter the numbers given above

number = eek.nextInt();

if (number > 0)

{

sum += number;

}

else if (number == 0)

{

done = true;

}

i++;

}

System.out.println(“end of test. “ + sum + “ “ + number);

}

}

Solution

Answer: end of test. 18 0

When user provides the inputs 5, 6 , –3 , 7 , –4 , 0, 5 , 8, 9 each vaue will tak it and try to add that value to the sum variable based on condition given above.

When value is 5 it will add to sum variabe so sum = 5

When value is 6 it will add to sum variabe so sum = 11

When value is -1 it will not add to sum variabe because there is a check to restrict negative values if (number > 0) this condition will allow positive values to add to the sum variable.

When value is 7 it will add to sum variabe so sum = 18

When value is 0, this block will exeute. so loop will terminate

else if (number == 0)

{

done = true;

}

Finally, Sum value is 18 and number value is 0 at the time of loop termination.

What is the output of the following program with the following data values entered as inputs ? NOTE: the program will only be run ONE time! 5, 6 , –3 , 7 , –4 ,
What is the output of the following program with the following data values entered as inputs ? NOTE: the program will only be run ONE time! 5, 6 , –3 , 7 , –4 ,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site