Question
I need help with the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the
I need help with 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 largest integer that is divisible by 2 is 0 The count of even integers in the sequence is 1 The sum of positive integers is 0
This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, compute the largest integer that is divisible by 2, count how many even integers are in the sequence (can be divided by 2, "num%2 == 0"), and compute the sum of positive integers (are greater 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.
I already have most of the code done, I just need to figure out how to find the largest integer divisible by 2. I have no idea how to do that. Here is my code below:
import java.util.Scanner;
public class Assignment2
{
public static void main (String[] args)
{
int even=0; //variable to store total of even integers
int nnum=0; //variable to store sum of positive integers
int count=0; //variable for positive integers count
int max=0;
int num; //variable to store keybaord input
Scanner scan = new Scanner(System.in);
do {num = scan.nextInt();
if (num%2== 0 && num>0) //finds even and positive integers and adds to even and nnum
{
even+=num;
nnum+=num;
if (max > num)
{max = num;}
}
else if (num < 0) //finds negative integers and adds to nnum
{
nnum+=num;
if (max > num)
{max = num;}
}
else if (num%2!= 0 && num>0) //finds odd and positive integers and adds to count and odd
{
count++;
even +=num;
if (max > num)
{max = num;}
}
else //finds any other positive integer and adds to count
{
count++;
if (max > num)
{max = num;}
}
}
while (num!=0);
System.out.println("The minimum integer is " + max);
System.out.println("The largest integer divisible by 2 is " + nnum);
System.out.println("The sum of positive integers is " + even);
System.out.println("The count of even integers in the sequence is " + (count)); //had to subtract one because count would always come up 1 over
}
}
I just need to figure out what code i need to add to find the largest integer divisible by 2 without using arrys, I will definitely rate your anser highly if it works. Thank you
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started