Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that accepts a series of positive integer numbers entered via the keyboard. The program should then calculate and display the count and sum of even and odd numbers separately. The program should stop when the user enters -1.


What I have tried:

Java
import java.util.*;

class Main {
    public static void main(String[] args) {
        int n1;
        Scanner scan = new Scanner(System.in);


        for(int i = 0; i <=6 ; i++) {
            System.out.println("Please enter a number: ");

            n1 = scan.nextInt();

            int even = 0;
            int odd = 0;

            if (n1 % 2 == 0) {
                even = n1 + even;
                System.out.println("even: "+ even);

            }
            else {
                odd = n1 + odd;
                System.out.println("odd: "+ odd);
            }
        }
    }
}
Posted
Updated 15-Dec-22 3:09am
v3
Comments
CHill60 15-Dec-22 6:20am    
We are more than willing to help you when you get stuck, but we are not going to do your assignment for you. Write some code and then tell us what the problem is.
Shaikha Albloushi 15-Dec-22 6:21am    
import java.util.*;
class Main {
public static void main(String[] args) {
int n1;
Scanner scan = new Scanner(System.in);


for(int i = 0; i <=6 ; i++){
System.out.println("Please enter a number: ");
n1 = scan.nextInt();



int even = 0;
int odd = 0;

if (n1 %2 == 0){
even = n1 + even;
System.out.println("even: "+ even);

}
else{
odd = n1 + odd;
System.out.println("odd: "+ odd);
}
}


}
}
CHill60 15-Dec-22 9:23am    
And what happens when you run your code? What is the problem?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Just posting your assignment will not get you anywhere.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Well, your code should:
  • Initialize with 0 four int variables, say: even_count, odd_count, even_sum, odd_sum.
  • Make a proper loop (that is, an infinite one, breakable by -1 input).
  • Inside the loop increment the counts and update the sums as appropriate.
  • After loop exit, show the values of the four variables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900