max min ODD numbers from a sequence without arrays - java

I am trying to get the max and min ODD numbers from sequence of inputs without using an array, please advise what should I add, I am using the below code:
System.out.print("Enter odd integers(even to Stop): " );
do {
num = input.nextInt();
sum += num;
} while (num % 2 != 0);
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
System.out.print("Minimum Odd Number: " + min + ";");
System.out.println("Maximum Odd Number: " + max);
System.out.print("Sum: " + sum);

Your logic to check the odd value, to possibly record it as a min or max, should be inside the loop:
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int num = input.nextInt();
while (num % 2 != 0) {
sum += num;
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
num = input.nextInt();
}
System.out.print("Minimum Odd Number: " + min + ";");
System.out.println("Maximum Odd Number: " + max);
System.out.print("Sum: " + sum);
As a comment, I initialize the min and max integers to the largest and smallest integer values, respectively. This ensures that we correctly capture the very first inputs, whatever they might be.

This will work. If there aren't any odd numbers given min and max will be 0:
Scanner input = new Scanner(System.in);
int num = 0, sum = 0, max = 0, min = Integer.MAX_VALUE;
System.out.print("Enter odd integers(even to Stop): ");
do {
num = input.nextInt();
if (num < min) {
min = num;
}
if (num > max) {
max = num;
}
sum += num;
} while (num % 2 != 0);
if(min % 2 == 0) min = 0;
if(max % 2 == 0) max = 0;
System.out.print("Minimum Odd Number: " + min + ";");
System.out.println("Maximum Odd Number: " + max);
System.out.print("Sum: " + sum);
input.close();

Related

Finding the lowest and largest number in a nested loop [duplicate]

This question already has answers here:
Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array
(3 answers)
Closed 2 years ago.
int smallest = 0;
int largest = 0;
int num;
for (int row=1; row<=5; row++)
{
System.out.println();
System.out.println("Grades for student #" + row);
num = (int)(Math.random()*50)+50;
double total = 0;
smallest = num;
for (int col = 0; col<10; col++)
{
num = (int)(Math.random()*50)+50;
System.out.print(num+ ", ");
total += num*1.0;
}
System.out.println();
double average = total/10;
System.out.println("Average for student is " +
average);
if (num > largest) {
largest = num;
}
if (num < smallest) {
smallest = num;
}
}
System.out.println("The largest score is:" + largest);
System.out.println("The smallest score is : " +
smallest);
Trying to figure print out the lowest and largest score for random numbers generated in a loop for 5 students. The code is printing out the wrong number for lowest score.
The smallest and largest number are initialized with 0. They should be initialized with the first value instead.
You should put this part inside the nested loop.
if (num > largest) {
largest = num;
}
if (num < smallest) {
smallest = num;
}
Otherwise they are only comparing num of the last column of each row.
below is a code snippet of finding the largest and smallest value from a loop.you may adjust it with your requirement.
int minMax = 0;
int smallest = 0;
int largest = 0;
for(int i = 1; i <= totalNumbers; i++){
System.out.print("Number " + i + ": ");
int inputNumbers = console.nextInt();
if(inputNumbers > largest){
largest = inputNumbers;
} else if (inputNumbers < smallest){
smallest = inputNumbers;
}
}
System.out.println();
System.out.println("Smallest = " + smallest);
System.out.println("Largest = " + largest);

Java - How to only count the inputted positive numbers

I am trying to find sum and averags of user inputed numbers and i also Need my program to only sum the positive numbers entered.
It needs to calculate only the positive numbers sum and ignore the negative inputs, would i put my num=0 or not?
import java.util.Scanner;
public class J12ForSumPos {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int maxNumbers, i, num, average;
int sum = 0;
System.out.print("Enter Max Numbers: ");
maxNumbers = console.nextInt();
System.out.println();
for (i = 1; i <= maxNumbers; i = i + 1) {
System.out.print("Enter Value " + i + ": ");
num = console.nextInt();
sum = sum + num;
}
average = sum / maxNumbers;
if (sum >= 0) {
System.out.println();
System.out.println("Sum: " + sum);
System.out.println();
System.out.println("Average: " + average);
System.out.println();
} else {
System.out.println("Sum is: " + sum * 0);
System.out.println();
System.out.println("Cannot Calculate Average - no positives entered");
}
}
}
You can try something like this:
Scanner console = new Scanner(System.in);
int maxNumbers = 0;
int totalSum = 0; // Sum of all numbers (positive and negative)
int totalAverage = 0; // Average of all numbers (positive and negative)
int positiveSum = 0; // Sum of all positive numbers
int positiveAverage = 0; // Average of all positive numbers
int positiveNumberCount = 0; // Amount of positive numbers entered
System.out.print("Enter Max Numbers: ");
maxNumbers = console.nextInt();
System.out.println();
for(int i=1; i<=maxNumbers; i=i+1)
{
System.out.print("Enter Value " + i + ": ");
int num = console.nextInt();
if(num >= 0) {
positiveSum = positiveSum + num;
positiveNumberCount = positiveNumberCount + 1;
}
totalSum = totalSum + num;
}
positiveAverage = positiveSum / positiveNumberCount;
totalAverage = totalSum / maxNumbers;
It's up to you to decide whether or not to include 0 as a positive or a negative number, or exclude it. In my example it's being treated as a positive number.

How to understand why this does not return the minimum value?

Please help to understand why, in the following, I didn't get output of minimum value in array. Using scanner for input value, the program should output also the minimum number.
** without using .sort.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);
}
}
I will suggest two improvements at the bare minimum.
Initialize your min to the first value. Right now you have min initialized to 0 and user never enters any number smaller than 0. That's why you keep getting 0 for min.
Don't run your loops 1000 times. Run it as many times as there are non zero elements.
See the following working snippet:
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
min = value;
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length && list[i] !=0; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);

how to exclude negative numbers from average calculation in while loop

I have a program that accepts user inputs and calculate Max, Min, and Average. The program closes when the user inputs any negative number. How do i exclude the negative number from the average calculation? Here is what i have so far.
// variable
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE ;
int count = 0;
double neg;
//creat scanner object
Scanner input = new Scanner(System.in);
//loop
while (n > 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
sum = sum + n;
count++;
ave = sum / count;
if(n<0) neg = n;
if(n>max && n >= 0 ) max = n;
if(n<min && n >= 0) min = n;
if(n>0) ave = n; }
System.out.print(" Average " + ave + "\n Maximum " + max + "\n Minimum " + min);
}
}
Add an if condition:
n = input.nextDouble();
if(n < 0)
break;
sum = sum + n;
The following code only sums the input numbers when n is not negative.
import java.util.Scanner;
public class sample {
public static void main(String[] args) {
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
int count = 0;
double neg;
Scanner input = new Scanner(System.in);
// loop
while (n > 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
if(n >= 0){
sum = sum + n;
count++;
}
if (n < 0)
neg = n;
if (n > max && n >= 0)
max = n;
if (n < min && n >= 0)
min = n;
if (n > 0)
ave = n;
}
System.out.print(" Average " + ave + "\n Maximum " + max
+ "\n Minimum " + min);
}
}
Try this:
double n = 1;
double ave = 0;
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE ;
int count = 0;
// create scanner object
Scanner input = new Scanner(System.in);
// loop until n is negative
while (n >= 0) {
System.out.print("Input an income (any negative number to quit): ");
n = input.nextDouble();
if (n >= 0) {
if (n > max) max = n;
if (n < min) min = n;
sum = sum + n;
count++;
}
}
if (count > 0)
ave = sum / (double) count;
System.out.print(" Average " + ave + "\n Maximum " + max + "\n Minimum " + min);

why is my count increasing when entering 0, it should exit the loop

I been working on this all night but couldn't make anything out it. I want my code to sum all the numbers the user enter, count how many times the user enters the number. then calculate the average.
and then find the max and min, easy right. well yeah if i was to be allowed to use arrays but this is for review and I hate while loops.
here's my code.
double integer = 1;
//double num = 0;
double sum = 0.0;
double average = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
// double char1=0;
double min = integer;
double max = integer;
// char letter = 'q';
while (integer != 0) {
System.out.println("Please enter an integer: ");
integer = input.nextInt();
sum += integer;
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
else if (integer < min)
min = integer;
}
Here's the output:
Please enter an integer:
3
The sum of your numbers is: 3.0
The number of values entered is: 1
Please enter an integer:
2
The sum of your numbers is: 5.0
The number of values entered is: 2
Please enter an integer:
1
The sum of your numbers is: 6.0
The number of values entered is: 3
Please enter an integer:
0
The sum of your numbers is: 6.0
The number of values entered is: 4
The average of your sum is: 1.5
The max integer is: 3.0
The min integer is: 0.0
when the count increases by 1 my average comes out wrong. but why is 0 been counted as part of count and why my min always output 0 and not what the user enters. any and all help is much appreciated.
p.s. i have tried numerous ways but it doesnt work. if i try to change my count to start at -1 it solves my problem at hand with average but the count increases anyways so i know its incorrect. also the min problem stays there.
thanks guys
You need to add if condition to avoid increment when you enter 0.
You can use this code
// setting starting min and max value.
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
while (integer != 0) {
System.out.println("Please enter an integer: ");
integer = input.nextInt();
sum += integer;
if (integer != 0) { // added if condition
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
if (integer < min) // changed 'else if' to 'if'
min = integer;
}
}
System.out.println("Max : " + max);
System.out.println("Min : " + min);
Try this:
For these cases it is best to use the conditional do while. And initialize min at the maximum value allowed.
double integer;
double sum = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
double min = Double.MAX_VALUE;
double max = 0;
do {
System.out.print("Please enter an integer: ");
integer = input.nextInt();
if (integer >0) {
sum += integer;
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
if (integer < min)
min = integer;
}
} while (integer != 0);
System.out.println("avg: "+sum/count);
System.out.println("max: "+max);
System.out.println("min: "+min);
you will need to add an extra if condition to make it work.
I have made few changes as below in your code and it is working as expected.
double integer = 1;
//double num = 0;
double sum = 0.0;
double average = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
double min = integer;
double max = integer;
while (true) {
System.out.println("Please enter an integer: ");
integer = input.nextInt();
if(integer != 0)
{
sum += integer;
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
else if (integer < min)
min = integer;
}
else
break;
}
double integer = 1;
double sum = 0.0;
double average = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
while (integer != 0) {
System.out.println("Please enter an integer(press zero to exit): ");
integer = input.nextInt();
if (integer > 0){
sum += integer;
count++;
if (integer > max)
max = integer;
if (integer < min)
min = integer;
}
}
System.out.println("The sum of your numbers is: " + sum);
System.out.println("Your count number is: " + count);
average = sum / count;
System.out.println("The average of your sum is: " + average);
System.out.println("The max integer is: " + max);
System.out.println("The min integer is: " + min);
}
}
You may try the below code
import java.util.Scanner;
public class ComputeDemo {
public static void main(String[] args) {
double integer = 1;
//double num = 0;
double sum = 0.0;
double average = 0.0;
Scanner input = new Scanner(System.in);
int count = 0;
// double char1=0;
double min = integer;
double max = integer;
// char letter = 'q';
while (integer != 0) {
System.out.println("Please enter an integer: ");
integer=input.nextInt();
if(integer>0)
{
sum += integer;
count++;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
if (integer > max)
max = integer;
else if (integer < min)
min = integer;
}
else
{
min=0;
System.out.println("The sum of your numbers is: " + sum);
System.out.println("The number of values entered is: " + count);
System.out.println("");
}
}
}
}

Categories