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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Sum of even and odd integers

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,943
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Member
    Join Date
    Jan 2012
    Posts
    57
    My Mood
    Fine
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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...

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 >.<

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,943
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Sum of even and odd integers

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

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,943
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Sum of even and odd integers


  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);

    // asks the question

    System.out.println("Please enter the number");
    User = Keyboard.nextInt();

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,943
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Sum of even and odd integers

    You need to continue with it so that it compiles. What you have will not compile.
    Add a println to print out the value that is read to be sure it works as expected.
    Compile it, and execute it to see if it is working as expected.
    Then add the next step.

    You are not following java naming conventions. Variable names start with lowercase letters, classnames with uppercase letters. This will be important as your programs get bigger to make it easier to read and understand what the code is doing.

Similar Threads

  1. Sending integers through a socket
    By Fricken Hamster in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2011, 06:44 PM
  2. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  3. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 07:38 PM
  4. java integers
    By timeline in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2010, 03:54 AM
  5. [SOLVED] Java program to convert and compare integers
    By luke in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 18th, 2009, 07:26 PM