# Averages of user inputed values (Need Help)

• 04-11-2008, 03:04 PM
Zebra
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:

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);     }   }```
• 04-11-2008, 03:30 PM
sanjeevtarar
Hello Zebra,

Please check with this code.

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
• 04-16-2008, 02:51 PM
Zebra
Thank you so much!