Jump to content

while loop (sum of odd integers)

- - - - -

  • Please log in to reply
31 replies to this topic

#1
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
How would I go about getting the sum of the odd numbers between the starting and ending numbers.
I got the easy part, the sum of all numbers between the first integer and second integer, but I need to sum all the odd integers between the first and second.


// ***********************
// *                     *
// * LAB05A.java, CSc 15 *
// *                     *
// ***********************
import java.util.Scanner;
public class Lab05A
{
 public static void main(String[] args)
 {
  // Variables
  int start;
  int end;
  int count;
  int sum;
  
  Scanner keyboard = new Scanner(System.in); //input - java scanner
  
  // PROGRAM START
  System.out.print("What is the beginning number: ");
  start = keyboard.nextInt();
  
  System.out.print("What is the end number: ");
  end = keyboard.nextInt();
  
  sum = 0;
  count = start;  
  
  
  while (count <= end)
  {
   sum = sum + count;
   count++;
  }
  
  System.out.println("The sum of all integers between " + start + " and " + end + " is " + sum + " (inclusively).");
  System.out.println("The count value is " + count + ".");
  
  
  
 
 }
}



#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
This look like a school homework, so I won't give you the answer directly, but you should take a look at a operation called modulus

#3
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

Vaielab said:

This look like a school homework, so I won't give you the answer directly, but you should take a look at a operation called modulus
I know I can do this with if statements and remainder, but is it possible to do with while loops?

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You almost have it, you only need to add a condition in your while loop.

Do you need to do this without an if statement and only while loop?

#5
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
What would be the conditional statement?

#6
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Come on, I can't give you the answer like that.
You have everything you need. You know the if as to be inside the loop
And you know that you will need a modulus.

So tell me, what variable change during the loop, that sometime is odd and sometime isn't?
And how do you know if a variable is odd with a modulus?

#7
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts
With the use of if inside while, would I be able to do if(sum%2!=0)?

#8
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
You have the good use of modulus, but not the good variable.
You have one variable that will be every number between the start and the end.
And you want to add it to the sum only when this variable is odd... so what variable is it?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Why not increment your counter twice in the loop instead of once?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
What if his first number is an even number?

#11
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts

Vaielab said:

What if his first number is an even number?

It still makes much more sense to increase the number by two from the efficiency side of view. just need to add the conditional statement, to get on the right track.
.

#12
GTghost

GTghost

    Newbie

  • Members
  • PipPip
  • 19 posts

Vaielab said:

You have the good use of modulus, but not the good variable.
You have one variable that will be every number between the start and the end.
And you want to add it to the sum only when this variable is odd... so what variable is it?
I think I am getting the concept you are trying to say, but I'm not sure how to write it in code.
So while count is less than or equal to end, and count is not divisible by 2, then I can 'sum = sum + count; ... etc.

But what if the count begins evens, how would I have it increment before getting the new sum?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users