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)
           {

            count++;
            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.

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.