2 Replies - 19292 Views - Last Post: 12 November 2008 - 10:37 PM Rate Topic: -----

#1 rockyw88  Icon User is offline

  • New D.I.C Head

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

sum of even and odd numbers

Post icon  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  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • 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;

Was This Post Helpful? 1
  • +
  • -

#4 rockyw88  Icon User is offline

  • New D.I.C Head

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

Re: sum of even and odd numbers

Posted 12 November 2008 - 10:37 PM

i got it thx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1