Write an application that finds the total and average of odd numbers from 1 to 15. Your program must use a counted loop to accomplish this task.

So far all I have is >

public class OddLoop { 


public static void main(String[] args) { 

{ 
int total = 1; 

for (int i = 3; i <= 15; i += 2) 
{ 

total = total * i; 

} 
System.out.println("The sum of the odd integers is:" + total); 
} 



} 
} 

Any Help will be greatly appreciated! Thank you for your time!

help with what?
you have everything you need. or don't you know the logic to calculate an average?

There are two problems I can see in the code, the main one being that you are multiplying the odd numbers rather than adding them. You also have an extra pair of brackets around the body of the main() method, which isn't a syntax error, but does confuse things somewhat. Proper indentation would make correcting that simpler.

public class OddLoop { 


    public static void main(String[] args) { 
        {   // note the extra bracket 
            int total = 1; 

            for (int i = 3; i <= 15; i += 2) { 

                total = total * i; 

            } 
            System.out.println("The sum of the odd integers is:" + total); 
        }   // note the extra close bracket
    } 
} 

Here's the code as I've corrected it:

public class OddLoop { 

    public static void main(String[] args) {  
        int total = 0; 

        for (int i = 3; i <= 15; i += 2) { 
            total += i;
        } 
        System.out.println("The sum of the odd integers is:" + total); 
    } 
} 

As for the logic for the average, the main thing you need that you don't have now is the number of odd integers which you are summing. This can be solved by adding another integer variable, count, which counts the number of loops performed. You then simply divided total by count to get the average.

Thanks, This is what I have now.

public class OddLoop {

public static void main(String[] args) {

int sum = 0;

//Find the sum
for (int i = 1; i <= 15; i+=2) { 
    sum += i;
}

//Find the average. 
double average = sum / 8; // 8 because there are 8 odd numbers between 1 and 15 //1, 3, 5, 7, 9, 11, 13, 15

System.out.println("The sum is " + sum + ". The average is " + average);
}
}

that works...but try not to hardcode values if possible. What I mean is the statement

double average = sum / 8;

Let's say you want to do the odd numbers 1 through 31? In this case it would divide by 8 and give you the wrong average.
An easy way to have this program apply to every number is to have a counter.

So, under your "int sum = 0;" at the top, "add int counter = 0;".
Then add "counter++" under your "sum+-i;" in the for loop.
This will make your counter variable equal to however many times your for loop runs, in this case that would be 8.

Then instead of saying "double average = sum /8;", say "double average = sum / counter;"

Do you need to do this? no, but it is a good way to practice for when you are given a problem without a definite number range, or changing numbers.

overall, it looks like this.

public class OddLoop {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int sum = 0;
        int counter = 0;
//Find the sum
        for (int i = 1; i <= 15; i += 2) {
            sum += i;
            counter++;
        }
//Find the average. 
        double average = sum / counter; // counter is keeping track of how many numbers for you
        System.out.println("The sum is " + sum + ". The average is " + average);
    }
}
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.