# for loop help

• 09-23-2012, 08:15 PM
kevintran
for loop help
I am given this objective
Quote:

Lab Description : Run a loop from a provided start number to a provided stop number. Total up all numbers in the group, determine all odds in the group, and determine all evens in the group.

Sample Data :
1 5
2 8
5 15

Sample Output :
1 5
total 15
even count 2
odd count 3

2 8
total 35
even count 4
odd count 3

5 15
total 110
even count 5
odd count 6

This is my code
Code:

```import java.util.*; public class lab08g {         public static void main(String[] args) {                 Scanner input = new Scanner(System.in);                 System.out.print("Number 1: ");                 int num1 = input.nextInt();                 System.out.print("Number 2: ");                 int num2 = input.nextInt();                 System.out.println(num1 + " " + num2);                 int total = 0, even = 0, odd = 0;                 for (int total1 = num1; total1 <= num2; total1++) {                         total = total + total1;                 }                 System.out.println("total " + total);                 for (int even1 = num1; even1 <= num2; even1++)                 {                         if (even1 % 2.0 == 0) {                                 even = even + even1;                         }                 }                 for (int odd1=num1;odd<=num2;odd1++)                 {                         if (odd1%2.0!=0)                                 odd=odd+odd1;                 }                         System.out.println("even count " + even);                         System.out.println("odd count "+odd);         } }```
My even and odd count don't show up right.
This is what my output would look like

Quote:

Number 1: 2
Number 2: 8
2 8
total 35
even count 20
odd count 15
What did I do wrong?
• 09-23-2012, 08:22 PM