Results 1 to 3 of 3
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default 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);
        }
      }
    I am a Java n00b.

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello Zebra,

    Please check with this code.

    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. #3
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 04-16-2008, 01:26 PM
  2. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  3. How to get the User Name
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:35 PM
  4. Replies: 1
    Last Post: 08-07-2007, 05:35 AM
  5. Help with removing somthing once inputed.
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-26-2007, 10:58 PM

Posting Permissions

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