Concept explainers
(Count positive and negative numbers and compute the average of numbers) Write a
Enter an integer, the input ends if it is 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total is 5.0
The average is 1.25
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
Count positive and negative numbers and compute the average of numbers
Program Plan:
- Include the required import statement.
- Define the class
- Define the main() method using public static main.
- Declare and initialize the required variables.
- Declare the input scanner.
- Read an input from the user.
- Using while loop, check whether the integer is “0” or not
- Check whether the integer is greater than “0”.
- If so, increment the positive counter.
- Check whether the integer is less than “0”.
- If so, increment the negative counter.
- Calculate the sum of integers.
- Read the next input.
- Check whether the integer is greater than “0”.
- Display the sum and average of integers.
- Define the main() method using public static main.
The below program is used to count number of positives and number of negatives which are presented as inputs and finally calculate its sum and average as follows:
Explanation of Solution
Program:
//import statement
import java.util.Scanner;
//class Excersise_1
public class Excersise_1 {
// main function
public static void main(String[] args) {
// declare and initialize the required variables
int count_Positive = 0, count_Negative = 0;
int counter = 0, sum = 0, integer;
// declare the input scanner
Scanner in = new Scanner(System.in);
// print the instruction
System.out.print("Enter an integer, the input ends if it is 0: ");
// read the integer value from user
integer = in.nextInt();
// using while loop, check the integer
while (integer != 0) {
// check if it is positive
if (integer > 0)
/* if so, increment the positive counter */
count_Positive++;
// check if it is negative
else if (integer < 0)
/* if so, increment the negative counter */
count_Negative++;
// calculate the total of integer
sum += integer;
// increment the counter
counter++;
// Read the next integer
integer = in.nextInt();
}
// check the counter is 0
if (counter == 0)
// if so, no inputs are read
System.out.println("No numbers are entered except 0");
else {
// print the number of positive integers
System.out.println("The number of positives is " + count_Positive);
// print the number of negative integers
System.out.println("The number of negatives is " + count_Negative);
// print the sum
System.out.println("The total is " + sum);
// print the overall average
System.out.println("The average is " + sum * 1.0 / counter);
}
}
}
Enter an integer, the input ends if it is 0: 1
2
-1
3
0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
Want to see more full solutions like this?
Chapter 5 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Starting Out with Python (4th Edition)
Starting Out with Java: Early Objects (6th Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with C++: Early Objects
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Java How To Program (Early Objects)
- user_num = int(input('Enter integer:\n')) Extend the given program as indicated. Output the user's input. Output the input squared and cubed. Get a second user input into user_num2, and output the sum and product.arrow_forward9- Write a program which read a number of 3 digits and check if (units, tens, hundreds) digit is greater than 5, convert this digit to 5. FOR eg. 147 After check 145 983 After check 553arrow_forward4: Format.cpp) Write a program that gets two real values from the user, prints their sum and product in scientific notation, and prints their sum and product in fixed point notation. Print answers to three decimal places.arrow_forward
- Write a program to check if a number is a prime number. 1) let user input a number 2) check if its legal input (must be greater than 1), if not, ask the user input again until the input is greater than 1. 3) check if the number is prime 4) print "it is prime" or "it is composite". (A natural number (1, 2, 3, 4, 5, 6, etc.) is called a prime number (or a prime) if it is greater than 1 and cannot be written as the product of two smaller natural numbers) prime number only has divisors, 1 and itself.arrow_forward[Calculate grades’ average for a student] write a program that calculates the student grades’ average for a semester for the number of courses taken in that semester. Your program should do the following: 1.Read from user the number of courses (n) 2. Then, read the courses’ grades for n times (Hint: use a loop) 3.If a grade is grater than 100 or less than 0, ask the user to enter the grade again. 4.Calculate the average of grades using the following formula: average = (sum of grades) / n 5.Print out the average grade on the screen. Note: Always use appropriate data types.arrow_forwardM=5 N=2 P=M//N print("P") What is the output of the above programarrow_forward
- H. W1: - Write a program to find the summation of n terms from the following series. x2 x6 Sum .... to n term y11 H. W2: - Write a program to find the summation of the following series. Sum = 5+112² + 175 + 237 + 299arrow_forward6- Write a program to input number of 3 digits and check any digit is even convert it to 3 and any digit is odd convert it to 4. FOR eg. 127 after check 434 359 after check 444 684 after check 333arrow_forwardWrite a code that prints the Fibonacci series backwards. Prompt for and input two integers in the series. Print out the series in reverse order starting from the biggest number entered down to zero. Check for “errors” entering the two numbers and correct when possible. At the end, print out whether or not the numbers entered were in fact two consecutive Fibonacci numbers. C Programarrow_forward
- (Financial: credit card number validation) Credit card numbers follow certain pat- terns. A credit card number must have between 13 and 16 digits. It must start with: 4 for Visa cards 5 for Master cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check, which can be described as follows (for illustra- tion, consider the card number 4388576018402626): 1. Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number. 4388576018402626 → 2 * 2 = 4 → 2 * 2 = 4 → 4 * 2 = 8 → 1 * 2 = 2 6 * 2 = 12 (1+ 2 = 3) → 5 * 2 = 10 (1+ 0 = 1) → 8 * 2 = 16 (1 + 6 = 7) → 4 * 2 = 8arrow_forward(Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. *********** USE PYTHON ********* Sample Run 1 Sample Output 1: Enter an integer, the input ends if it is 0: 1 Enter an integer, the input ends if it is 0: 2 Enter an integer, the input ends if it is 0: -1 Enter an integer, the input ends if it is 0: 3 Enter an integer, the input ends if it is 0: 0 The number of positives is 3 The number of negatives is 1 The total is 5 The average is 1.25 Sample Run 2 Sample Output 2: Enter an integer, the input ends if it is 0: 0 No numbers are entered except 0arrow_forward1. Use if - else - elif Read 3 numbers from the user and check the following. () If the 1st number is more then 2nd number, add both the numbers. (1i) If the 2nd number is less than 3rd number, find the product of both the numbers and print the product. (iii) If the 15t number is more than 3rd number, print the square of both the numbers. 2. Use loops to calculate the following series. 1210/xY + 1311/ x + 1412/x +...n-2 /x using any 1 loop of your choice.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr