Java programming
1. Write a program that reads an unspecified number of integers, determines how many positive and negative value 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
Here are sample runs: (red indicates a user input)
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
Continue? (y/n) y
Enter an integer, the input ends if it is 0: 0
No numbers are entered except zero.
Continue? (y/n) y
Enter an integer, the input ends if it is 0: 2 3 4 5 0
The number of positives is 4
The number of negatives is 0
The total is 14 The average is 3.5
Continue? (y/n) y
Enter an integer, the input ends if it is 0: -4 3 2 -1 0
The number of positives is 2
The number of negatives is 2
The total is 0
The average is 0.0
Continue? (y/n) n
Good bye!
My current code: ( I am missing the y/n prompts, and I am stuck on how to Keep it from crashing):
int positive = 0;
int negative = 0;
int total = 0;
double average = 0;
System.out.print("Enter an integer, the input ends if it is 0:
");
Scanner input = new Scanner(System.in);
for (int buffer = -1; buffer != 0; ) {
buffer = input.nextInt();
if (buffer > 0) positive++;
else if (buffer < 0) negative++;
total += buffer;
}
if (positive + negative == 0) {
System.out.println("No numbers are entered except 0");
System.exit(0);
}
average = total /(double)(positive + negative);
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total is " + total);
System.out.println("The average is " + average);
System.out.println("Continue? (y/n): ");
ANSWER:-
import java.util.*;
class NumberProgram
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
// scanner class for user input
int positive,input,count; //
variable declaration
int negative; // variable
declaration
int total; // variable
declaration
char ch; // variable
declaration
double average; // variable
declaration
do // do while loop begin
{
total=0; //
initialize total with 0
positive=0;
negative=0;
count=0;
average=0;
System.out.println("Enter an integer, the input ends if it is 0 :
"); // give message for user for input
do
{
input=sc.nextInt(); // take
integer input
if(input>0) // check input
is greater than 0 or not
{
positive++; // count the
positive number
count++;
}
if(input<0)
// check input is less than 0 or not
{
negative++;
count++; // count
the positive number
}
total+=input; // sum of all
numbers
}while(input!=0); // check input is 0 or not if
input 0 then loop exit
average=(double)total/count; // find
average
if (positive + negative == 0) { // check the
numbers available or not
System.out.println("No
numbers are entered except 0");
}
else
{
System.out.println("The
number of positive is "+positive); // print positive number
count
System.out.println("The
number of negative is "+negative); // print negative number
count
System.out.println("The Total
is "+total); // print total
System.out.println("The
average is "+average); // print average
}
System.out.println("Continue?
(y/n): "); // give message for user for choice
ch= sc.next().charAt(0); // take
input y/n
}while(ch=='y' || ch=='Y'); //
check user input if y then again exicute same process
System.out.println("Good bye!"); //
when program end then display good bye
}
}
// If any doubt please comment
OUTPUT:-
Java programming 1. Write a program that reads an unspecified number of integers, determines how many...
Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...
This is a Java program Write a program that reads an unspecified number of integers, determines home 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 to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...
(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. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...
8. What is the output of this program if the input: 1 2 3 4 -5 0 26? importjava.util.Scanner; public class Q_09 Your answer: public static void main(String[] args) int n, neg_count = 0, sum = 0; Scanner input = new Scanner(System.in); System.out.println("Enter n:"); n = input.nextInt(); while(n !=0) neg_count++; sum +=n; System.out.println("sum =" + sum); System.out.println("#of negatives:" + neg_count + "\t" +"Total = "+sum);
I need to write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100. PreLab07 Analyze scores Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how...
(While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).
I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...
How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...
1. What is the output of each of the following code segments if the inputs are as follows: 5 0 15 -5 2 Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”); int number = input.nextInt(); if(number <= 0) break; sum += number; } System.out.println(sum); Second Segment: Scanner input = new Scanner(System.in); int sum = 0; for(int i = 0; i < 5; i++){ System.out.print( “Enter a number”);...
Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...