How to determine odd or even number?

L

Lin

How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,
 
J

Jim

How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

Test for odd is easy,

public static boolean isOdd(int x) {
return (x && 1) == 1;
}

Note that that will work for digits also.

I'll leave the test for zero to the poster :)

Jim
 
A

Andrew Thompson

How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

N.B. '0' is even.

Loop the String from 0 to length into parts using String.substring.
Integer.parsInt the parts, use '%2' on the integer to get a 0 or 1 result.
Increment one of two counters depending on the value of the modulus.

Your question indicates to me that
a) You should be posting on a different group, described here..
<http://www.physci.org/codes/javafaq.jsp#cljh>
b) You will probably not understand the terse explanation above.

Post to the suggested group and I will help you further.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
P

Paul Lutus

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Since this is your homework assignment, you need to post your code, not the
description of your assignment.
 
G

George W. Cherry

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

There are 10,000 ways to do this. The following suggests
one way but doesn't give the output as your homework
assignment specified. You might compile the following,
play with it, and then adapt it to give the output as the
above specifies. (If you don't do some original work your
professor or his staff may catch you doing what you're
doing.)

class AnalyseDigits {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
analyseDigits(number);
}

public static void analyseDigits(int number) {
int n = Math.abs(number);
do {
int digit = n%10;
if (digit == 0) {
System.out.println(digit + " is zero.");
}
else if (digit%2 == 0) {
System.out.println(digit + " is even.");
}
else {
assert (digit%2 != 0); // Take out this line before you compile.
System.out.println(digit + " is odd.");
}
n = n/10;
} while (n != 0);
}
}
 
M

Mike Schilling

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

How will you learn anything if you don't do your own homework?
 
T

Tor Iver Wilhelmsen

How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

Type into an editor and save the file.
odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Who wrote that assignment and decided 0 wasn't an even number? Why
doesn't the output match the description? (It doesn't show the count
but the digits.)

Solution: Loop using /10 as decrementor, extract digits using %10,
increment count variables based on %2, and show at end. Still
confused? Take shop instead of programming.
 
S

Sudsy

Tor Iver Wilhelmsen wrote:
Who wrote that assignment and decided 0 wasn't an even number? Why
doesn't the output match the description? (It doesn't show the count
but the digits.)

Solution: Loop using /10 as decrementor, extract digits using %10,
increment count variables based on %2, and show at end. Still
confused? Take shop instead of programming.

Sounds like a worthwhile recommendation. In these parts you're much more
likely to secure gainful employment (at excellent wages!) in the trades
than in the IT field.
Ever look at what an electrician or plumber makes?
You might be surprised!...
 
G

Gary Labowitz

Tor Iver Wilhelmsen said:
(e-mail address removed) (Lin) writes:
Solution: Loop using /10 as decrementor, extract digits using %10,
increment count variables based on %2, and show at end. Still
confused? Take shop instead of programming.

Don't recommend that! Have you ever tried to wire a three way switch? It
requires the same kind of logic as a couple of NAND and OR circuits. I
wouldn't want my house wired by a guy who can't figure out modulus operator!
 
G

George W. Cherry

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

Does this assignment have an application to
cryptography?

At any rate, here's the 9,756th way of doing your
homework (except for the output, which I leave
up to you).

class AnalyseDigits2 {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
analyseDigits(number);
}

public static void analyseDigits(int number) {
String n = Integer.toString( Math.abs(number) );
for (int index = 0; index < n.length(); index++) {
char digit = n.charAt(index);
switch (digit) {
case '0':
System.out.println(digit + " is zero (and even!).");
break;

case '1': case '3': case '5': case '7': case '9':
System.out.println(digit + " is odd.");
break;

case '2': case '4': case '6': case '8':
System.out.println(digit + " is even.");
break;

default: // You had better delete this line
assert (false); // and this line to get a clean compile.
} //end switch selection
} // end for loop
} // end analyseDigits method
}// end AnalyseDigits2 class
 
T

Thomas G. Marshall

Jim coughed up:
Test for odd is easy,

public static boolean isOdd(int x) {
return (x &&
&

1) == 1;
}

Note that that will work for digits also.

I'll leave the test for zero to the poster :)

Jim
 
T

Thomas G. Marshall

Gary Labowitz coughed up:
Don't recommend that! Have you ever tried to wire a three way switch?
It requires the same kind of logic as a couple of NAND and OR
circuits. I wouldn't want my house wired by a guy who can't figure
out modulus operator!

Agreed lol. 3-way switches, WHICH BY THE WAY HAVE ONLY 2 FREAKING SWITCHES,
are not intuitive.
 
S

steve

How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

it is very simple divide the number by 2, and check if there is a remainder.
2/2 =1 (no remainder)
3/2=1( 1 left over)
4/2=2 ( no remainder)

steve
 
M

michael

a crazy way of doing this.

int i = 123456;
String tk[] = String.valueOf(i).split("1|3|5|7|9");
for ( int j =0 ;j<tk.length;j++){
System.out.println("odd:" + tk[j]);
}

thanks
Michael
 
G

George W. Cherry

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

Your professor's specification is ambiguous: it does not
state what to print when the integer number contains
repeated digits. The following program's output repeats
any repeated digits. The program analyzes '0' (zero) as
an even digit. Have you turned in your assignment yet?
(I give your professor a failing grade if he specified the
program as you describe it above.)

George

class AnalyzeDigits3 {
public static void main(String[] args) {
int number = Integer.parseInt(args[0]);
String[] data = analyzeDigits(number);
System.out.println(data[0]);
System.out.println(data[1]);
}

public static String[] analyzeDigits(int number) {
String n = Integer.toString( Math.abs(number) );
StringBuffer evenDigits = new StringBuffer("even digits:");
StringBuffer oddDigits = new StringBuffer("odd digits:");
for (int index = 0; index < n.length(); index++) {
char digit = n.charAt(index);
switch (digit) {
case '0': case '2': case '4': case '6': case '8':
evenDigits.append(
( lastCharInBufferIsDigit(evenDigits) ? ", " : " ") + digit
);
break;

case '1': case '3': case '5': case '7': case '9':
oddDigits.append(
( lastCharInBufferIsDigit(oddDigits) ? ", " : " ") + digit
);
break;
} //end switch selection
} // end for loop
return new String[]{evenDigits.toString(), oddDigits.toString()};
} // end analyzeDigits method

private static boolean lastCharInBufferIsDigit(StringBuffer buffer) {
return Character.isDigit( buffer.charAt( (buffer.length() - 1) ) );
}
}// end AnalyzeDigits3 class
 
G

George W. Cherry

Lin said:
How do you write a java program to determine and prints the number of
odd, even, and zero digits in an integer?

For example, I have an integer number: 256401

The print out should look like:

odd digit: 5, 1
even digit: 2, 6, 4
zero digit: 0

Thanks,

I'd love to know how some of the heavy hitters here
would program this.
 
C

Chris Uppal

George said:
I'd love to know how some of the heavy hitters here
would program this.

I don't know whether I count as a heavy hitter, but my reply is that I wouldn't
program this. The trivial reason is that its a totally unrealistic exercise,
and I feel no need to mess with such. The more important reason is that it's
underspecified in several ways.

One -- as you have already mentioned -- is that the spec doesn't, um, specify
what to do with repetitions.

Another is that the special-casing of zero sounds suspicious to me -- zero is
even in my book. As a professional programmer I'd want to go back to the
"customer" (or analyst if there was one, and s/he struck me as competent) to
check that they really understood what they were calling for.

A third is that the spec seems to call for the input being an integer, not a
String as most people have assumed in their replies. An integer has many
natural representations, none of which are implicit in the value itself. E.g.
these all represent the same integer:
0x00010
0x10
16
000016

How many '0's are there ? (And should 'x's be counted ?) What about negative
numbers ? OTOH, if the input /is/ a String, how are non-digit characters to
be handled ? Can the String be in hex format ? Is the String representing
real Unicode, or can I assume there's a one-to-one mapping between Java 'char's
and actual characters ? Should I allow non-Western representations of numbers
?

The next problem is that I don't know the context where the code is going to be
used (and I can't make a reasonable guess either, because the problems is so
artificial). Is it going to be in the tightest part of the inner loop of an
interpreter that will run the whole application ? Is it just going to be run
once or twice a year as part of an error-reporting routine. Somewhere
in-between ? I don't know whether to optimise the code for clarity or
speed -- or, more accurately, I don't have the information to give good weights
to those potential virtues (either /could/ be zero).

Lastly, even treating it as an exercise, I don't know what I'm supposed to be
demonstrating my knowledge of. Should I just create a nasty little C-style
static method containing a loop, or should I demonstrate real OO, and create a
DigitsCount class, with responsibility for performing the calculation, and
holding the results ? Should I design that class in XP style (YAGNI, and that)
so that it /only/ accepted a String to decode and /only worked in decimal, or
should I approach it in library programmer style, and attempt to cover all the
legitimate uses of the object (e.g. accepting text streams and CharSequences as
sources of the data to analyse, and having an optional 'base' parameter) ?

I'd be interested to hear of any other reasons for refusing the assignment ;-)
I /think/ I've covered the bases, but I suspect I can be proved wrong....

-- chris
 
A

Andrew Thompson

C

Chris Uppal

Andrew said:
In that list of reasons I did not see..
'The OP is not paying me enough to do it, and has not shown enough
effort to justify a serious response on c.l.j.programmer.'

Drat!

-- chris
 
A

Andrew Thompson


But then, I did also note in my first response* that the
OP should be posting to a different group, and added..

"Post to the suggested group and I will help you further."
<http://google.com/groups?as_ugroup=comp.lang.java.help&as_uauthors=Lin>

Still.. I suppose they've handed in George's homework by now,
passed the assignment and (hopefully) dropped Java. ;-)

* <http://google.com/[email protected]>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top