Java This assignment will be the construction of a program that reads in an unsp
ID: 3661908 • Letter: J
Question
Java
This assignment will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the inputted numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:
The minimum integer is 0
The count of even integers in the sequence is 1
The sum of negative integers is 0
This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, count how many even integers (can be divided by 2, "num%2 == 0") are in the sequence, and compute the sum of negative integers (are less than 0).
Note that the above is an output for the first test case. For other test cases, you will have different numbers.
Do not prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.
Explanation / Answer
while(data != 0){
if(data>0) {
if(data%2==0){ // if it is even then increase even count
evencount++;
}
else
{ System.out.println(“ it is an odd”);
}
}
elseif(data <0){
sum_negative +=data; // if it is negative add the number to the sum count.
if(data %2==0) // if it is even and negative add to even count again
{ evencount++;
} else
{ System.out.println(“ enter the next number”);
data = input.nextInt();
}
}
}
System.out.println(“ the minimum number entered is 0”);
System.out.println ("The sum of negatives is " + sum_negative);
System.out.println ("The number of even numbers is " + evencount);
}
}
OUTPUT:
Enter an int value(the program exits if input is 0): 1 2 4 -4 6 8 -2 0
The minumum number is 0
The sum of negatives is -6
Th number of even numbers is 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.