In: Computer Science
Assignment #2 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 maximum integer is 0
The largest even integer in the sequence is 0
The count of negative integers in the sequence is 0
The sum of even integers is 0
This means that using all numbers your program reads (including the last number 0), you need to compute the maximum, compute the largest even integer (can be divided by 2, "num%2 == 0"), count how many negative integers are in the sequence, and compute the sum of even integers.
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.
Code
import java.util.Scanner;
public class Assignment2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int maxInt=0,largestEven=0,negativeCount=0,evenSum=0;
int num;
while(true)
{
num=sc.nextInt();
if(num==0)
break;
if(maxInt<num)
maxInt=num;
if(num%2==0)
{
evenSum+=num;
if(largestEven<num)
largestEven=num;
}
if(num<0)
negativeCount++;
}
System.out.println("The maximum integer is "+maxInt);
System.out.println("The largest even integer in the sequence is
"+largestEven);
System.out.println("The count of negative integers in the sequence
is "+negativeCount);
System.out.println("The sum of even integers is "+evenSum);
}
}
outputs
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.