This content has been marked as final. Show 9 replies
Mohamad Samy wrote: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.
can I get a code to get the sum of the odds and sum of evens at user input 5 integers.
Tip: stick to the New to Java forum while you are learning.
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:
Now write the code, we are Watching :p
I am really appreciating your quick reply.
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.
after that dynamic reply, I'll be adhered to just a very helpful forum.
Does that code satisfy the problem
// That program is to get the sum of the odds and evens for five integers input by user.
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");
System.out.println("pls, enter 2nd integer");
System.out.println("pls, enter 3rd integer");
System.out.println("pls, enter 4th integer");
System.out.println("pls, enter 5th integer");
System.out.printf("Sum of odds is %d and Sum of evens is %d, SumOdd, SumEven)
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.
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