10 Replies - 2595 Views - Last Post: 18 September 2013 - 09:45 PM Rate Topic: -----

#1 shawk23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

need help while loop sum of odd integers

Posted 15 September 2013 - 09:32 PM

need help computing the sum of odd integers between 1 and 9. (**using a while loop) what i have so far is:
public class HW1
{
    public static void main(String[] args)
    {
        int sum = 0, n = 1;

        while (n <= 9  )
        { 
           
            if(n%2 !=0);
            else
            if(true)
            sum = sum + n;
            n++;
            System.out.println(sum);
        }  
         
            
        
    }
}

This post has been edited by jon.kiparsky: 15 September 2013 - 09:38 PM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: need help while loop sum of odd integers

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: need help while loop sum of odd integers

Posted 15 September 2013 - 09:43 PM

You start out reasonably well, but what happened here?


            if(n%2 !=0);
            else
            if(true)
            sum = sum + n;
            n++;
            System.out.println(sum);




1. The semicolon after the if condition is actually an instruction, it means "do nothing". So that line would be equivalent to
if(n%2 !=0)
  {
    ;  // empty instruction: do nothing if n is odd
  }



2. else what?
if (true)

always executes, because the condition is always true. So if n is NOT odd, you're adding n to sum.

3. Then you increment n, regardless of what else has happened, and finally you print the sum.


So I'm not sure what logic is happening here, but those are some things to look at.
Was This Post Helpful? 2
  • +
  • -

#3 shawk23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:07 PM

View Postjon.kiparsky, on 15 September 2013 - 09:43 PM, said:

You start out reasonably well, but what happened here?


            if(n%2 !=0);
            else
            if(true)
            sum = sum + n;
            n++;
            System.out.println(sum);




1. The semicolon after the if condition is actually an instruction, it means "do nothing". So that line would be equivalent to
if(n%2 !=0)
  {
    ;  // empty instruction: do nothing if n is odd
  }



2. else what?
if (true)

always executes, because the condition is always true. So if n is NOT odd, you're adding n to sum.

3. Then you increment n, regardless of what else has happened, and finally you print the sum.


So I'm not sure what logic is happening here, but those are some things to look at.



Thank you late night programming after a long day leads to silly mistakes....
now this is where i'm at not sure where my problem is here

class HW2
{
   public static void main(String[] args)
   {
      int n = 1, sum = 0;
      while(n <= 9)
      {
         if(n%2 !=0)
         sum = sum + n;
         n++;
         System.out.println(sum);
      }
   }
}

This post has been edited by jon.kiparsky: 15 September 2013 - 10:23 PM
Reason for edit:: code tags

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:24 PM

Is there a question in there somewhere?
Was This Post Helpful? 0
  • +
  • -

#5 shawk23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:37 PM

View Postjon.kiparsky, on 15 September 2013 - 10:24 PM, said:

Is there a question in there somewhere?


Sorry new to this..
import java.util.Scanner;
public class HW1
{
    public static void main(String[] args)
    {
        int sum = 0, n;
        Scanner kb = new Scanner(System.in);
        n = kb.nextInt();
       
        while (n < 1)
        { 
           
            if(n%2 !=0);
           
            sum = sum + n;
            n++;
            System.out.println(sum);
        }  
         
            
        
    }
}


I have figured out the first part of my question and in part two I am asked to compute the same as above (sum of odd integers) but ask for the conditions. When I enter the same conditions as in part 1 (n=1, x=9) I do not get the same result as in the first.
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: need help while loop sum of odd integers

Posted 15 September 2013 - 10:43 PM

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?
Was This Post Helpful? 2
  • +
  • -

#7 shawk23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

Re: need help while loop sum of odd integers

Posted 18 September 2013 - 03:48 PM

View Postjon.kiparsky, on 15 September 2013 - 10:43 PM, said:

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?


I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.
    public static void main(String[] args)
    {
        int sum = 0, n, count = 1;
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter n");
        n = kb.nextInt();
        while (count <= n)
        { 
           
            if(sum%2 !=0)
                {
                sum = sum + count;
                count++;
                }
            else ;  
        }
        System.out.println(n + sum);
    }
}

Was This Post Helpful? -1
  • +
  • -

#8 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:05 PM

View Postshawk23, on 18 September 2013 - 10:48 PM, said:

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.


It's cool. We've all been there. Here's how you have to play the game for a little bit though:

import java.util.Scanner;

public class Client {
	public static void main(String[] args) {
		int sum = 0, n, count = 1;
		Scanner kb = new Scanner(System.in);
		System.out.println("Enter n");
		n = kb.nextInt();
		System.out.println("dbg: n = " + n);
		while (count <= n) {
			System.out.println("dbg: count = " + count + ", n = " + n);
			if (sum % 2 != 0) {
				System.out.println("dbg: sum = " + sum + " & sum%2 != 0");
				sum = sum + count;
				System.out.println("dbg: sum(changed) = " + sum);
				count++;
				System.out.println("dbg: count = " + count);
			} /*else
				; you don't need that...*/
		}
		System.out.println(n + sum);
	}
}



Stick with it and we'll show you some new tricks along the way but everyone starts like that.
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10690
  • View blog
  • Posts: 18,307
  • Joined: 19-March 11

Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:30 PM

View Postshawk23, on 18 September 2013 - 05:48 PM, said:

View Postjon.kiparsky, on 15 September 2013 - 10:43 PM, said:

Take a closer look at your while loop condition. What happens if I enter some value >1 for n?


I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.



You can use debug statements as MentalFloss suggests, or you can step through the code in your head. Specifically, look at what happens in the code if you enter a value like 7 at the prompt.

int n = 7;
 while (count <= n)
 {
   System.out.println("Entered the loop!");
 }



Will that println ever execute? Try to answer it by examination of the code, it's not that difficult.

(Sorry, didn't mean to hit the downrep button... I'll make it up elsewhere)

This post has been edited by jon.kiparsky: 18 September 2013 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

#10 shawk23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-September 13

Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:44 PM

View PostMentalFloss, on 18 September 2013 - 09:05 PM, said:

View Postshawk23, on 18 September 2013 - 10:48 PM, said:

I am now stuck again, I am new to this... brand new in fact I just can't seem to figure out where I am going wrong.


It's cool. We've all been there. Here's how you have to play the game for a little bit though:

import java.util.Scanner;

public class Client {
	public static void main(String[] args) {
		int sum = 0, n, count = 1;
		Scanner kb = new Scanner(System.in);
		System.out.println("Enter n");
		n = kb.nextInt();
		System.out.println("dbg: n = " + n);
		while (count <= n) {
			System.out.println("dbg: count = " + count + ", n = " + n);
			if (sum % 2 != 0) {
				System.out.println("dbg: sum = " + sum + " & sum%2 != 0");
				sum = sum + count;
				System.out.println("dbg: sum(changed) = " + sum);
				count++;
				System.out.println("dbg: count = " + count);
			} /*else
				; you don't need that...*/
		}
		System.out.println(n + sum);
	}
}



Stick with it and we'll show you some new tricks along the way but everyone starts like that.






Thank you , but I am still not printing the results I want. I am trying to obtain the sum of odd integers up to n that is entered. For example if i enter n = 5 I should yield the result 25.
I am getting an infinite loop still.
Was This Post Helpful? 0
  • +
  • -

#11 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: need help while loop sum of odd integers

Posted 18 September 2013 - 09:45 PM

OK well, just know that the only spot you are incrementing count which is a condition of your while is in a location that does not get hit on first pass so it will never increment and thus it is an infinite loop.

It jumps out at you if you have a hojillion print statements blazing by but it's hard to see if you've never programmed before.

This one's just not ready for "mentally stepping through code".
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1