9 Replies Latest reply: Mar 6, 2013 8:27 PM by J2EE_Life

sum of odds and evens

Hi, I am new to this great language.
I am just a beginner following How to program book
can I get a code to get the sum of the odds and sum of evens at user input 5 integers.
• 1. Re: sum of odds and evens
can I get a code to get the sum of the odds and sum of evens at user input 5 integers.
Yes, but you need to study a little more and then write it. If you have some code you are having trouble with, feel free to post it and ask for more specific help. But don't expect people to do the work for you, that's not the way it works.

Tip: stick to the New to Java forum while you are learning.
• 2. Re: sum of odds and evens
I will give you algorithm that you can follow and cover-up with java syntax but as said above write ur own Code and if u get some specific problem or don't know how to do something then ask. This is for your own good.

1. Provide a provision for user to enter 5 integers.
2. Read the entered integers and possibly put them in array.
3. Declare two separate variables (one to get sum of even, SUM_EVEN and other to get sum of odd,SUM_ODD).
4. Iterate over the array.
5. If integer is odd, add to SUM_ODD else to SUM_EVEN.
6. Display the result.

For array you can refer to below link:

[http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html]

Now write the code, we are Watching :p
• 3. Re: sum of odds and evens
my way is actually to type every program I wanna make and not to simply get the code from expertise.
but at the book How to program, I was asked to use only ch.3 techniques which is only If statement not even if else or an array just u mentioned.
I will try your method using only if statement.
• 4. Re: sum of odds and evens
Does that code satisfy the problem
// That program is to get the sum of the odds and evens for five integers input by user.
import java.util.Scanner
public class OddEven
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int a, b, c, d, e, f, SumOdd, SumEven;
System.out.println("pls, enter first integer");
a=input.nxtInt();
System.out.println("pls, enter 2nd integer");
b=input.nxtInt();
System.out.println("pls, enter 3rd integer");
c=input.nxtInt();
System.out.println("pls, enter 4th integer");
d=input.nxtInt();
System.out.println("pls, enter 5th integer");
e=input.nxtInt();
if((a%2)==0)
SumEven=a;
SumOdd=a;
if((b%2)==0)
SumEven=a+b;
SumOdd=a+b;
if((c%2)==0)
SumEven=a+b+c;
SumOdd=a+b+c;
if((d%2)==0)
SumEven=a+b+c+d;
SumOdd=a+b+c+d;
if((e%2)==0)
SumEven=a+b+c+d+e;
SumOdd=a+b+c+d+e;
System.out.printf("Sum of odds is %d and Sum of evens is %d, SumOdd, SumEven)
}
}
• 5. Re: sum of odds and evens
I've entered my code example and I wanna it revised by forum expertise.
• 6. Re: sum of odds and evens
Hi, please post code using the [code]code here [/code] tags.
And your code is not compilable. There's no point in posting it here if it doesn't compile.
• 7. Re: sum of odds and evens
Good that u tried. Now correct urself.

Correction Needed for:

import java.util.Scanner --> Don't u think punction(;) is missing?
input.nxtInt(); --> nxtInt() is not correct method. Replace with correct one.
SumOdd = 0, SumEven= 0 --> these 2 variables should be initialized to zero as they are printed later on and their initialization later depends on if conditions which might not be true.

if((a%2)==0)
SumEven=a;
SumOdd=a;

Think what above 3 lines would do? U will need else also here. This comment is for all similar if conditions.

SumOdd=a+b+c+d+e; --> Try googling for += operator
System.out.printf("Sum of odds is %d and Sum of evens is %d, SumOdd, SumEven) --> missing semicolon and quotes.

Now give a try again
• 8. Re: sum of odds and evens
J2EE_Life wrote:
Good that u tried. Now correct urself.
Its "you", not "u". I thought I would let you know, its a very common word so you make the mistake a lot. Luckily it is a short word, no effort to do correctly. Right?