# Sum of even and odd integers

• February 6th, 2012, 02:59 AM
spikezone2004
Sum of even and odd integers
Hello all, I just started a new java class and I am having a little bit off a problem with this program I have to write. I need to have a program that reads an integer value from the user and displays the sum of all the even integers from 1 to the input value and the odd sum of all the integers both being inclusive. I get the java part of it for the most part I just can't think of the math part to calculate this. I know I need to use JOption and parse stuff but can't even begin to think of the math part that does this, any help? thank you.

Example: User enters 7.
Sum of even integers = 2+4+6 = 12
Sum of odd integers = 1+3+5+7=16
• February 6th, 2012, 06:18 AM
Norm
Re: Sum of even and odd integers
Can you explain what you mean by "the math part"?
Do you know how to add one number to another and save the results in a variable?
Do you know how to determine if a number is even or odd? See the % operator.
• February 6th, 2012, 02:32 PM
dx8292
Re: Sum of even and odd integers
stick with scanner instead of joptionpane

for(int i=0;i<=what ever number;i++)
{
new keyboard=new Scanner(System.in);

System.out.print("Enter a number: ");
enternumber=keyboard.nextInt();

.................................................. ......
if (i%2==0) //your condition for odd/even

by the way you need an accumulator:
int sum=0;
sum+=enternumber;

//this isn't the correct accumulator because it would be totaling all the inputs together...you need to find a way to accumulator the odds/evens.

The stuff above are pieces of the code...you need put these pieces of code with the additional code you have to come up by yourself.
• February 7th, 2012, 12:26 AM
deependeroracle
Re: Sum of even and odd integers
you can use this logic

if the mod ( % ) of no is == 0 the no is even .. and use a variable (say) sum=sum+no;
initially sum will be declared as 0
and no. is the no. that is provided by the user..
if the no mod ( % ) is not equal to zero then in the else part use the same code say sm = sm+no;
and print both of them after the loop and if statement block...
• February 8th, 2012, 04:11 PM
spikezone2004
Re: Sum of even and odd integers
oh man, now i am lost. I'm not sure what to use at all anymore. Starting from scratch but I'm not to sure where to start >.<
• February 8th, 2012, 04:14 PM
Norm
Re: Sum of even and odd integers
Do things one small step at a time.
What is the first thing you need to do?
• February 8th, 2012, 04:28 PM
spikezone2004
Re: Sum of even and odd integers
create a class?

This is the first java class I have taken, and my teacher isn't the best he answers questions with questions, half the people in the class are in the same boat as me.
• February 8th, 2012, 04:30 PM
Norm
Re: Sum of even and odd integers
• February 8th, 2012, 04:38 PM
spikezone2004
Re: Sum of even and odd integers
I took a look at it, makes sense. I haven't had any trouble with my past assignments, just this one because it is involving math its got me completely confused. My first assignment was to take the average of 5 numbers that the user inputs and I did that one fine, but this is confusing me
• February 8th, 2012, 04:58 PM
spikezone2004
Re: Sum of even and odd integers
this is what I have so far,

class integer
{

public static void main(String[] args)

// user variable
{
int User;

// input for keyboard

Scanner Keyboard = new Scanner(System.in);