Question
Modify Examine1 code to use an if...if/else statement. Check each value entered by the user to determine if the user entered a negative number. If
Modify Examine1 code to use an if...if/else statement.
Check each value entered by the user to determine if the user entered a negative number. If so, program should count each negative number entered.
Once user has entered all numbers, program should print the total negative numbers entered by user.
Your program should print the average using the printf command and the number of negative numbers entered.
Note: You may include or exclude the negative numbers with your calculation.
This is the code for Examine1
import java.util.Scanner;
public class Examine1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many numbers would you like to enter? ");
int count = input.nextInt();
int sum = 0;
for (int i = 0; i < count; i++) {
System.out.print("Enter number #" + (i+1) + ": ");
int num = input.nextInt();
sum += num;
}
double average = (double) sum / count;
System.out.printf("The sum of the %d numbers is %d. ", count, sum);
System.out.printf("The average of the %d numbers is %.2f.", count, average);
input.close();
}
}
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