# sum of even and odd numbers

Page 1 of 1

## 2 Replies - 19292 Views - Last Post: 12 November 2008 - 10:37 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=71659&amp;s=f29424f6bb0569d530ea0ab25e18b8c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rockyw88

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 28-October 08

# sum of even and odd numbers

Posted 12 November 2008 - 06:37 PM

Write a program which accepts user input. This program will show the SUM of the even numbers entered and the SUM of all of the odd numbers entered. The user can enter as many numbers as they wish

```import javax.swing.JOptionPane;

public class test
{
public static void main(String[] args)
{
int odd, even, Ototal, Etotal;

String oddStr, evenStr, outputStr;

oddStr =
JOptionPane.showInputDialog("Enter A Group Of Numbers: ");
odd = Integer.parseInt(oddStr);

Ototal = odd + odd;
Etotal = odd = odd;
outputStr = "You Entered:  " +odd  + "\n"
+"Sum of odd numbers: " + Etotal +"\n"
+"Sum of even numbers: " + 2;

JOptionPane.showMessageDialog(null, outputStr,
"CONGRATULATIONS",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}
```

i got most of it, just not the part where it adds the odd OR even numbers ....

Is This A Good Question/Topic? 0

### #3 Jayman

• Student of Life

Reputation: 421
• Posts: 9,532
• Joined: 26-December 05

## Re: sum of even and odd numbers

Posted 12 November 2008 - 08:09 PM

The modulas operator (%) will do all the work for you. It returns the remainder after dividing two numbers. So any number evenly divisible by 2 is even, any numbers not evenly divisible by 2 are odd numbers.

Example:
```//Divide the input by 2, the modulas operator will divide and return the remainder.
//If the remainder is 0, then the number was evenly divided by 2. Otherwise
//there is a remainder, so the number is odd.
if ((odd % 2) == 0)
Etotal = Etotal = odd;
else
Ototal = Ototal + odd;
```

### #4 rockyw88

• New D.I.C Head

Reputation: 0
• Posts: 15
• Joined: 28-October 08

## Re: sum of even and odd numbers

Posted 12 November 2008 - 10:37 PM

i got it thx