I have write a program to read 6 numbers and find the average of all odd numbers. here is my code

Scanner get = new Scanner (System.in);

        double average = 0, counter = 0 ;
        int i, num = 0, sum = 0, count = 0, total = 0;

        for(i=0; i<=5; i++)

           if(i % 2 == 1)

            sum += num;

        System.out.println("the sum of odd numbers is "+ total);
        System.out.println("the average is " + average);

I tried different approach and still not getting it to work, this is my last approach for now. can u tell me where i am going wrong here.

3 Years
Discussion Span
Last Post by Taywin

Firstly, i is just an integer that you use to loop through with. It isn't the collection of numbers that the user entered. So checking
if(i % 2 == 1)
doesn't help you with your average because it has nothing to do with the input.


the only thing that is "wrong" with it, is that you haven't finished yet.
if you want us to be more specific, explain how you notice that it's wrong. do you get an error message ? does it compile ?


Also one more hint... Your code has no input from user. You need to add that and use the input to determine your results, not i.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.