Results 1 to 7 of 7
  1. #1
    kmlam585 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Adding odd numbers

    This is probably something very basic, but I can't for the life of me think of how to get this to work.

    What I need to do is prompt the user for a positive integer(n), then add n odd numbers together (eg: n=4; add 1+3+5+7 = 16)

    I know I need to loop through 4 times: for(int i = 0; i < n; i++)
    I can add up sequential numbers, just can't seem to get it right when I only need odd numbers.

    I'm stuck on this problem, can someone help me with this?

    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding odd numbers

    If you have to loop n times, the largest number in your loop is 2*n-1 (check this); you only want to do odd numbers so you have to start at one (1) with an increment of 2; so:

    Java Code:
    for (int i= 1; i <= 2*n-1; i+= 2)
       // do your dirty deed here ...
    But, if you apply a bit of math, you don't have to loop at all; adding the numbers 1+3+5+ ... +2*n-1 is n*n

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    kmlam585 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Adding odd numbers

    yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:

    Write a program that reads in a positive integer N and then calculates and displays
    the sum of the first N odd integers. For example, if N is 4, your program should
    display the value 16, which is 1 + 3 + 5 + 7.
    I'm trying to answer this by printing out the loop (ex: i = 0 and total = 1, i = 1 and total = 3, i = 2 and total = 5, i = 3 and total = 7) Then I'll have a separate variable to add total for find the final answer of 16.

    I want to break it down into the it's smallest parts so I can get a better understanding of why and how it's doing what it's doing.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding odd numbers

    Quote Originally Posted by kmlam585 View Post
    yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:



    I'm trying to answer this by printing out the loop (ex: i = 0 and total = 1, i = 1 and total = 3, i = 2 and total = 5, i = 3 and total = 7) Then I'll have a separate variable to add total for find the final answer of 16.

    I want to break it down into the it's smallest parts so I can get a better understanding of why and how it's doing what it's doing.
    See the loop in my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    kmlam585 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Adding odd numbers

    Thanks for the help! I was thinking about it completely wrong.

    Java Code:
    import acm.program.*;
    
    public class AddOddInts extends ConsoleProgram {
    
        public void run() {
            int n = readInt("Enter a positive integer: ");
            int total = 0;
            for (int i = 1; i <= 2 * n - 1; i++) {
                if (i % 2 != 0) {
                    total += i;
                    //println(i + " Odd " + total);
                }
            }
            println(total);
        }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: Adding odd numbers

    Almost perfect; if i is an odd number, then i+2 also is an odd number. You can change your loop to this:

    Java Code:
    for (i= 1; i <= 2*n-1; i+= 2)
       total+= i;
    Variable i only has odd values so you can skip the test in the body of your for loop.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    kmlam585 is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Adding odd numbers

    ahhh, ok that makes sense to me. I was doing a lot of this, but not putting it together correctly. I see how it works now though.

    thanks for your help JosAH!

Similar Threads

  1. adding numbers in a string?
    By brycepower1 in forum New To Java
    Replies: 14
    Last Post: 08-19-2011, 05:39 PM
  2. adding numbers
    By droidus in forum New To Java
    Replies: 7
    Last Post: 03-09-2011, 04:26 AM
  3. adding numbers in an array together
    By pds8475 in forum New To Java
    Replies: 3
    Last Post: 01-22-2011, 07:23 PM
  4. Adding numbers in an array?
    By hawaiifiver in forum New To Java
    Replies: 9
    Last Post: 01-22-2009, 03:50 AM
  5. Adding numbers in array
    By Shaolin in forum New To Java
    Replies: 1
    Last Post: 11-15-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •