Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

design a flowchart that prints number of odd,even and zero digits in

Status
Not open for further replies.

narri

MIS
Nov 11, 2002
4
US
Design a flowchart that prints number of odd,even and zero digits in an INTEGER value read from the User.
 
Is this a question or a directive ?

The voice of tek-tips speaks. "Thou shalt go forth into the world and desgin a flowchart ...".
 
Sorry,i was trying to write this program in JAVA.so i am interested in knowing how to handle number entered by user and check how many odd,even and zero digits ae there in the Number entered by user.
 
To work out whether a number is odd or even, use a modulus :

Eg

int i = ... //however you read the input

if ((i % 2) == 0) {
// even
} else {
// odd
}
 
sedj:
nono!
narri is asking for 'digits' not Numbers.

narri:
If it is UserInput, it could be a String - couldn't it?
Have a look at Pattern, Regexp and Matcher.

I would replace all odd numbers by nothing and compare the strings lengths.

Code:
// user input:
String number = "1345778101";

String sOdd = ("[13579]");
String sNull = ("0");

String mOdd = number.replaceAll (sOdd, "");
String mNull = number.replaceAll (sNull, "");

int nulls = number.length () - mNull.length ();
int evens = number.length () - mOdd.length () - nulls;
int odds =  number.length () - (nulls + evens);

Or - to make sedj happy :) -
Iterate over the String, and switch:
Code:
// ...
switch (input.charAt (i))
{
    case '0': ++nulls; break;
    case '2':
    case '4':
    case '6':
    case '8': ++evens; break;
    // if no '-' or '.' is in the number
    default : ++odds; 
}
 
stefanwagner :

erm ... " digits in an INTEGER value ". I though an integer was a number !
 
Hi,
Thanks to one and all.
After i read Integer Number from the User for example user enters 6789479.i need to know how many odd,even and zero digits are there in the number entered above.


So we need to read each digit and then use
int i = ... //however you read the input

if ((i % 2) == 0) {
// even
} else {
// odd
}
so looks like we need to read each number.
If anyone can let me know how to red each numbr and say number%2==0.
 
Maybe something a bit like :

Code:
String numberIn = "6789479";
char[] chars = numberIn.toCharArray();
for (int j = 0; j < chars.length; j++) {
  int i = (int)chars[j];

  if ((i % 2) == 0) {
    // even
  } else {
    // odd
  }
}
 
Code:
	public static void oddEvenNull_sw (String input)
	{
		int evens = 0, odds = 0, nulls = 0;
		for (int i = 0, max = input.length (); i < max; ++i)
		{
			switch (input.charAt (i))
			{
			    case '0': ++nulls; break;
			    case '2':
			    case '4':
			    case '6':
			    case '8': ++evens; break;
			    // if no '-' or '.' is in the number
			    default : ++odds;
			}
		}
		System.out.println ("odd: " + odds + "\teven: " + evens + "\tnull: " + nulls);
	}
	
	public static void main (String args[])
	{
		oddEvenNull_sw	("678900479");
	}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top