Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Sum of even and odd integers

1. ## 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  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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...  Reply With Quote

6. ## 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 >.<  Reply With Quote

7. ## Re: Sum of even and odd integers

Do things one small step at a time.
What is the first thing you need to do?  Reply With Quote

8. ## 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.  Reply With Quote

9. ## Re: Sum of even and odd integers  Reply With Quote

10. ## 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  Reply With Quote

11. ## 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);  Reply With Quote
12. ## Re: Sum of even and odd integers  Reply With Quote