Odd and Even
Question Description
i have this code.. but i dont know how to add all the even numbers and odd numbers enter by the user.
Example output:
There are 6 even numbers : 12, 8,28, 26, and 16
The sum of even numbers: _______
There are and 4 odd numbers : 1, 3,15, and 11
The sum of odd numbers: _____
---------------------------------------------------------------------------------------------------------------------------------------------import java.util.Scanner;
public class OddEven {
public static void main(String[] args) {
private static Scanner kb = new Scanner(System.in);
System.out.println("\nProgram to get 10 integers and display the odd and even numbers.\n");
System.out.println("Enter the numbers:");
int n;
int[] odd = new int[10];
int[] even = new int[10];
int oddCnt, evenCnt;
oddCnt = evenCnt = 0;
for (int k = 0; k < 10; k++) {
System.out.print((k + 1) + ") ");
n = Integer.parseInt(kb.nextLine().trim());
if (n % 2 == 0) {
even[evenCnt++] = n;
} else {
odd[oddCnt] = n;
oddCnt++;
}
}
System.out.print(
"\nThere are " + evenCnt + " even numbers: ");
int k = 0;
for (k = 0; k < evenCnt - 1; k++) {
System.out.print(even[k] + ", ");
}
System.out.println(" and " + even[k]);
System.out.print("There are " + oddCnt + " odd numbers: ");
k = 0;
for (k = 0; k < oddCnt - 1; k++) {
System.out.print(odd[k] + ", ");
}
System.out.println(" and " + odd[k]);
}
}
This question has not been answered.
Create a free account to get help with this and any other question!
Similar Content
Ezperanza Rising
by Pam Muñoz Ryan
Brave New World
by Aldous Huxley
Fences
by August Wilson
Dr Jekyll And Mr Hyde
by Robert Louis Stevenson
The 48 Laws of Power
by Robert Greene
All the Kings Men
by Robert Penn Warren
The Grapes of Wrath
by John Steinbeck
Pachinko
by Min Jin Lee
Breakfast at Tiffanys
by Truman Capote
Studypool values your privacy. Only questions posted as Public are visible on our website.
Brown University
1271 Tutors
California Institute of Technology
2131 Tutors
Carnegie Mellon University
982 Tutors
Columbia University
1256 Tutors
Dartmouth University
2113 Tutors
Emory University
2279 Tutors
Harvard University
599 Tutors
Massachusetts Institute of Technology
2319 Tutors
New York University
1645 Tutors
Notre Dam University
1911 Tutors
Oklahoma University
2122 Tutors
Pennsylvania State University
932 Tutors
Princeton University
1211 Tutors
Stanford University
983 Tutors
University of California
1282 Tutors
Oxford University
123 Tutors
Yale University
2325 Tutors