Results 1 to 8 of 8
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Sum of odd ints 0-n

    I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.

    Everything should be ok except these blocks, which is where I'm guessing I'm having the issues...
    ... global variables
    Java Code:
    int n = 0;
    int result = 0;
    int newnum = 0;
    int count = n;
    
    //... computational code
    
    while(count>=0) {
           if(n % 2 ==1)  {              //if n is odd
            result = n + newnum;     //result updated
            newnum = n;                 //newnum equal to new odd int n
            n=n - 2;                        //n set to next odd number down
            count = count - 1;          //count updated
           }
           Else if(n % 2 == 0) {     //if n is even
            n = n - 1;                    //make n odd
            count = count - 1;        //continue subtracting counter
           }
    }
    
    system.out.println(result)
    system.exit(0)
    //
    So far, it does the first step of the program correctly as far as I can tell. If n is set to 5, it displays 8 (5+3). if n = 7, it displays 12 (7+5)
    Why doesn't it continue?
    Thanks in advance

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    What do you actually get when you run the program? Why the count variable? Wouldn't using n>=0 work better for the while loop? You seem to have a lot of unnecessary variables in your code.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Since count is the variable that will change each time through the loop, count is the variable you should check for evenness or oddness, and count is the variable you should add to result if it's odd. If it's even, you should do nothing and go on to the next iteration of the loop. I don't see any purpose for newnum.

    -Gary-

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    By the way, if you're allowed to know anything about even and odd numbers when doing this exercise, you might profit by knowing that the odd integers start at 1 and go up by twos. Are you allowed to use a for loop?

    -Gary-

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    int count = n;
    int result = 0;
    if ((count%2) ==0)
    count --;

    while (count > 0)
    {
    result = result+count;
    count = count -2;
    }
    system.out.println(result)


    is it useful?

  6. #6
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    So, how about this code? it's doing the same thing..
    Java Code:
    ...
    int n = 0;
    int result = 0;
    ...
    while(n>=0) {
            if(n%2==1) {
            result = result + n;
            n = n-2;
           }
            else {
            n=n-1;
    }
    System.out.println(result);

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

    Default

    Quote Originally Posted by falkon114 View Post
    I'm trying to write a program for my class that computes the sum off all odd integers between 0 and n, a user defined arbitrary variable.
    The solutions I've seen here are all so 'computerese'; the old Greek didn't have computers but they were smarter than we are nowadays; they could think and the only tools they had were pebbles in the sand. They did this:

    Java Code:
        
     *                <-- one pebble
    
            
     *   *            <-- three more pebbles
            
     *   *  
    
                
     *   *   *        <-- five more pebbles
                
     *   *   *  
                
     *   *   *  
    
                    
     *   *   *   *    <-- seven more pebbles
                    
     *   *   *   *  
                    
     *   *   *   *  
                    
     *   *   *   *
    If you add nine more pebbles you get another square shape. Suppose there are P(n) pebbles in a square; if you add n+1 more pebbles you have P(n+1) == P(n)+n+1 pebbles. The length of a side of a square with P(n) pebbles is (n+1)/2. So P(n)= (n+1)*(n+1)/4. So given n (odd), the sum of all the numbers 1+3+5+6+7+ ... == (n+1)*(n+1)/4. We don't need no steenkin' loops.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks for that pointer. I've been trying to think of an "equation" to represent it. I figured there had to be one, but.. Im not good with math =/
    (yet)
    rewrote my code, I realised my made it way too complex the first go around. here tis:
    Java Code:
    while(n>0) {
    	if(n%2==1) {
    		result = result + n;
    		n=n-2;
    	}
    	else {
    		n=n-1;
    	}
    	
    }
    System.out.println(result);
    
    }
    }

Similar Threads

  1. Converting ints to Strings Problem
    By gkoef in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 01:07 PM
  2. Help with ints
    By Insomniac Riot in forum New To Java
    Replies: 5
    Last Post: 04-02-2010, 03:53 PM
  3. checking for ints in a String
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 03-26-2010, 06:09 PM
  4. GUI's and inputting doubles or ints
    By lopder1 in forum New To Java
    Replies: 19
    Last Post: 11-05-2009, 08:50 PM
  5. reading in unsigned ints into a 2D array
    By newToIt in forum New To Java
    Replies: 9
    Last Post: 03-06-2009, 12:36 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
  •