We wish to develop a (java) program that will count the number of even and odd i
ID: 673061 • Letter: W
Question
We wish to develop a (java) program that will count the number of even and odd integers in a set ("even" meaning divisible by 2, "odd" meaning not divisible by 2). We will use zero as an indicator that the set has been completely entered, and this zero should not be counted as part of the set. Ask the user for a sequence of integers, terminated by zero. Output the number even integers and the number of odd integers.
I really need help with java logic, and coming up with an algorithm in Java, so any suggestions would be really appreciated! Thanks
Explanation / Answer
import java.io.*;
class cnt_odd_evn
{
public static void main(String args[]) throws IOException
{
int[] a=new int[100];
int e=0,od=0,i,j,n,b;
String s;
DataInputStream o = new DataInputStream(System.in);
for(i=0;i<100;i++)
{
System.out.println("Enter Number");
s=o.readLine();
n=Integer.parseInt(s);
if(n!=0)
a[i]=n;
else
break;
}
for(j=0;j<i;j++)
{
b=a[j]%2;
if(b==0)
{
e=e+1;
}
else
{
od=od+1;
}
}
System.out.println("Even count ="+e);
System.out.println("Odd count ="+od);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.