Avg/Max from ArrayList - java

I've had a ton of issues with this program, added a while hasNextInt, multiple other things and i've finally got the code working, but my output is just the first input, instead of max/avg of the inputs. Could somebody let me know where i'm messing up?
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;
int num = scnr.nextInt();
if (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
num = scnr.nextInt();
}
int avg = count == 0 ? 0 : total/count;
System.out.println(avg + " " + max);
}
}

you are not using a loop to get numbers from the console. Also, logic for avg may result in the wrong answer. find below solution.
Scanner scnr = new Scanner(System.in);
int count = 0, max = 0, total = 0;
System.out.println("Enter any other characters expect numbers to terminate:");
while(scnr.hasNextInt()) {
int num = scnr.nextInt();
if (num >= 0) {
count++;
total += num;
max = Math.max(max, num);
}
}
double avg = count == 0 ? 0 : (double)total/(double)count; // to print correct avg
System.out.println(avg + " " + max);
sample Output:
Enter any other characters expect numbers to terminate:
3
3
34
a
13.333333333333334 34

Related

count the number of each prime factor of a number

I want to count the number of each prime factor of an integer then print the highest number of counts. e.g. 720 = 2^4 * 3^2 * 5^1. so I want to print Maximum Number of Time occurrence = 4 and the number is 2. I try to use Integer.max(), but that didn't work.
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
TreeMap<Integer, Integer> factors = new TreeMap<>();
for (int i =2; i < num; i++){
int count = 0;
while (num%i == 0){
System.out.println(i + " ");
num = num/i;
count ++;
}
if (count > 0){
factors.put(i, count);
}
}
if (num > 2){
System.out.println(num + " ");
factors.put(num, 1);
}
System.out.println( "-------------" );
for( Integer factor : factors.keySet() ){
System.out.println( factor + "^" + factors.get( factor ) );
}
}
}
If you just want the prime which occurs most then you can just check after each prime has been tested. For this, a map is not really necessary. Note that multiple primes may occur the same number of times. If there is a tie, this only saves the first one encountered.
int num;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num = in.nextInt();
int primeFactor = 0;
int count = 0;
int maxCount = 0;
int inc = 1; // initial increment.
int i = 2; // intitial prime
while (num > 1) {
count = 0;
while (num%i == 0) {
num = num/i;
count++;
}
// here check current count against maxCount and
// alter if required.
if (count > maxCount) {
maxCount = count;
primeFactor = i;
}
if (count > 0) {
System.out.printf("Prime = %d, count = %d%n", i, count);
}
i+=inc; // bump by 1 to get 3 and then
inc = 2; // start incrementing by 2
}
// and print the results.
System.out.println("First highest occuring prime = " + primeFactor);
System.out.println("MaxCount = " + maxCount);

Sum and average of numbers in Java

Here is the code but it's not optimized for all the exceptions. For example, if the user types 0 from the beginning, it will give the division by zero error and also I don't know why the sum of the numbers work.
Basically, I make num = 1 so it's different than 0 so the while loop can start, but in the end, I made sum = sum - 1; but it gave a wrong number.
Please help me.
import java.util.Scanner;
class Main {
public static void main (String[] args) {
int count = 0;
int num = 1;
double average = 0.0;
int soma = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Type a integer. 0 to exit.");
while (num != 0) {
num = scanner.nextInt();
sum = sum + num;
count++;
}
if (num == 0) {
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
}
}
}
change while to do while:
do {
num = scanner.nextInt();
sum = sum + num;
count++;
}while (num != 0);
Change FROM:
average = sum / (count -1);
System.out.println("The sum of the numbers is: "+ soma);
System.out.println("The average of the numbers is: "+ media);
TO:
average = sum / (count);
System.out.println("The sum of the numbers is: "+ sum);
System.out.println("The average of the numbers is: "+ average);
it should work

While loop even count

my problem is the following. If I input number 2, the code counts it as an odd number.
Remainder for 2 / 2 = 0 so the error doesn't make sense.
Below is the program:
import java.util.Scanner;
public class Ohjelma {
public static void main(String[] args) {
// Tänne voit kirjoittaa ohjelmakoodia. Ohjelmasi voit ajaa
// valitsemalla menusta Run->Run File tai painamalla Shift+F6
Scanner reader = new Scanner(System.in);
System.out.println("Type numbers: ");
int number = Integer.parseInt(reader.nextLine());
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
while (number != -1) {
System.out.println("Type numbers: ");
sum = sum + number;
number = Integer.parseInt(reader.nextLine());
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + many);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + even);
System.out.println("Odd numbers: " + odd);
The main problem is that for the critical part of your program it largely ignores the first input, apart from adding it to the running sum. You want to recast it like this:
Scanner reader = new Scanner(System.in);
int sum = 0;
int many = 0;
double average = 0;
int even = 0;
int odd = 0;
do {
System.out.println("Type numbers: ");
number = Integer.parseInt(reader.nextLine());
if (number == -1)
break;
sum = sum + number;
many++;
average = (double)sum / many;
if (number%2 == 0) {
even++;
} else {
odd++;
}
} while (true);
This will certainly processes even and odd numbers correctly.
Your code reads the second line of input into number before it checks whether number is odd ... and -1 is odd.

Count even and odd intergers until 0 is encountered

Here is what I have to do:
"Write a segment of code that reads a sequence of integers from the keyboard until the user enters a negative number. It should then output a count of the number of even integers and the number of odd integers read (not including the final negative value in either count). Remember - 0 is an even number. For example, if the sequence is:
2
7
15
5
88
1243
104
-1
Then the output should be
Number of even integers: 3
Number of odd integers: 4
My code just keeps going even after inputting -1. I have a feeling I am missing a {somewhere or wrote the code wrong. Here is my code:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
while (oddCount>=0&&evenCount>=0){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount = evenCount + 1;
else oddCount = oddCount + 1;
while (temp>0);
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
}
}
I think using do While loop will also prevent your isses,
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
(temp % 2==0)? evenCount++:oddCount++;
}
} while (temp > 0);
Your loop will always continue because while (oddCount>=0&&evenCount>=0) will always be true in your case. Try it like this:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
boolean continue = true;
while (continue){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount++;
else
oddCount++;
}
else {
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
continue = false;
}
}
Your code reads just one integer, then stuck at this line: while (temp>0);
The code below solve your problem:
int oddCount = 0, evenCount = 0, temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
while (true) {
temp = in.nextInt();
if (temp < 0) {
break;
} else if (temp % 2 == 0) {
evenCount = evenCount + 1;
} else {
oddCount = oddCount + 1;
}
}
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
There are a few problems here.
First, since you never decrease oddCount and evenCount, the condition oddCount>=0&&evenCount>=0 will always be true.
Second, you have an empty loop that loops endlessly, since it's condition is true and it has no body: while (temp>0);
I'd just take out the first loop, which is redundant, and use a do while loop:
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
if (temp % 2==0) {
evenCount++;
} else {
oddCount++;
}
}
} while (temp > 0);
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
Try this..
Scanner s=new Scanner(System.in);
int evenCount=0, oddCount=0;
while(true)
{
System.out.println("Enter a number");
int n=s.nextInt();
if(n<0) break;
if(n%2==0) evenCount+=n;
else oddCount+=n;
}
System.out.println("even count "+evenCount);
System.out.println("odd count "+oddCount);

How to summarize numbers entered by user?

I am doing an assignment and have most of what i think will work down, i have to promt the user for random numbers, then display how many negatives as well as positives then sum and average, can anyone help me as i cannot get my code to display the correct amount of values for negative or positive or get it to sum. Here is what i have so far( i also added the break in the end , otherwise it went to an infinate loop)
//Random number evaluation
package chapter_4;
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / data);
break; }
}
}
You are not asking for the nextInt() within the while loop - you do want to ask more than once, correct?
Your average is not an average. I suggest dividing by count instead of data.
at the end of the loop (instead of the break) add data = input.nextInt();
btw for average you should display sum/(double)count (the cast to double is there so you'll see the fraction)
This might help you.
Explanation: Using a do-while loop might be easier because it asks for input and than checks the input. Also put the input statement inside the do-While loop so it asks for multiple inputs.
public static void main(String[] args)
{
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
int data = 0;
do
{
System.out.print("Enter in a value, if 0 is entered program stops: ");
data = input.nextInt();
sum += data;
count ++;
if(data < 0)
negative ++;
else if(data > 0)
positive ++;
}
//Stops if the value of data is ZERO(0) and continues if it's not
while(data != 0);
System.out.println("Positive Numbers = " + positive);
System.out.println("Negative Numbers = " + negative);
System.out.println("Sum of Numbers = " + sum);
System.out.println("Total Numbers = " + count);
}
import java.util.Scanner;
public class Four_One {
public static void main(String[] args) {
int positive = 0;
int negative = 0;
int sum = 0;
int count = 0;
Scanner input = new Scanner(System.in);
System.out.print("Enter in a value, if 0 is entered program stops: ");
int data = input.nextInt();
while (data != 0) {
sum += data;
if (data < 0)
negative++;
else if (data > 0)
positive++;
count++;
data = input.nextInt();
}
System.out.println("The number of positives is: " + positive);
System.out.println("The number of negatives is:" + negative);
System.out.println("The total is: " + sum);
System.out.println("The average is: " + sum / (double)count);
}
}

Categories

Resources