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
Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
Additional Engineering Textbook Solutions
Web Development and Design Foundations with HTML5 (8th Edition)
Electric Circuits. (11th Edition)
Concepts Of Programming Languages
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
SURVEY OF OPERATING SYSTEMS
Problem Solving with C++ (10th Edition)
- 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_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(IN C LANGUAGE) Cumulative Addition: Computer selects a number between 7 and 23 at random. User will only add 2, 3 or 5 numbers to reach that number. For example: To reach 14:User will enter 5 5 2 2 (4 input).Also he can enter 2 2 2 2 2 2 2 (7 input) or 3 3 3 3 2 (5 input)arrow_forward
- ) /₹3soos alspaarrow_forwardM=5 N=2 P=M//N print("P") What is the output of the above programarrow_forward(PYTHON) A Krishnamurthy number is a number which sum of the factorial of its digits is equal to the number itself. For example: Let us consider the number 145. Factorial sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. Therefore 145 is a Krishnamurthy number. Other examples include: 1, 2, 40585. Write a program that does the following: • asks the user to input an integer. • computes whether the number is a Krishnamurthy number. • then finally prints the result. Note: You are not allowed to use the built-in function math.factorial.arrow_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_forwardPython Programming Part 2 Direction: Use PYTHON to solve this problem. Description: This part tests for two things. The program must identify all resistors in series or in parallel with each other and calculate the equivalent resistance for each group of resistors. Recall that total resistance of resistors in series are added up while the total conductance of those in parallel are added up. Input Format: The first line of the input consists of 1 integer N which is the total number of resistors. The next N lines will consist of 3 strings and an integer R each separated by a space. The first string is the resistor name, the second and third strings are the node names where the resistor terminals are connected, and R is its resistance value. Output Format: One line for each group of resistors in series or parallel. Each line starts with a list of resistors arranged in lexicographical order (see Lexicographic order - Wikipedia) followed by their total resistance rounded off to the nearest…arrow_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
- 5- Write a program which read a number consists of 4 digits and find the maximum digit of the number. FOR eg. 1472 the maximum digit is 7 the maximum digit is 9 9835arrow_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_forwardQ1/ write a program to enter a number from(1-100) and find sum odd number onlyarrow_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