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
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.
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.
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...
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 >.<
Re: Sum of even and odd integers
Do things one small step at a time.
What is the first thing you need to do?
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.
Re: Sum of even and odd integers
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
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();
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.