# Thread: Averages of user inputed values (Need Help)

1. ## Averages of user inputed values (Need Help)

Hey. I need some help with this one:

"Write a program that gets n numbers from a user. The program then displays two averages. One average is that of all the even numbers that were entered, and the second is for the odds."

RULES:
-No more than one loop
-No more than one if-else statement.
-Output must say The average of inputed values is... (Example: The average of 23, 21, 19, and 25 is 22.)

I am having trouble. I can get it where it does the average of all numbers, but I can't get it to where it does the evens and odds and outputs them like (The average of 23, 21, 19, and 25 is 22). Can somebody please help.

Here is the code I have so far:

Java Code:
```import java.util.Scanner;
class EvenOddAlex {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
double avg;
double sum = 0;
int num;
int count = 0;
System.out.println("How many numbers? ");
int n = scan.nextInt();
for(int i = 1; i <= n; i++)
{
System.out.println("Enter a number");
num = scan.nextInt();
{
count = count + 1;
sum = sum + num;
}
}
avg = sum/n;
System.out.println("The average of " + n + " is " + avg);
}
}```

2. Hello Zebra,

Java Code:
```import java.util.Scanner;
class EvenOddAlex {
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
double avgEven,avgOdd;
double sumEven = 0,sumOdd=0;
int num;
int countEven = 0,countOdd=0;
int val = 2;
System.out.println("How many numbers? ");
int n = scan.nextInt();
for(int i = 1; i <= n; i++)
{
System.out.println("Enter a number");
num = scan.nextInt();
{

if(num%val==0){
countEven = countEven + 1;
sumEven = sumEven + num;
}
else{
countOdd = countOdd + 1;
sumOdd = sumOdd + num;
}
}
}
avgEven = sumEven/countEven;
avgOdd = sumOdd/countOdd;

System.out.println("The average of Even is : " + avgEven);
System.out.println("The average of Odd  is : " + avgOdd);
}
}```

sanjeev

3. Thank you so much!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•