Skip to content

Instantly share code, notes, and snippets.

@imryan
Created October 30, 2013 16:50
Show Gist options
  • Save imryan/7236031 to your computer and use it in GitHub Desktop.
Save imryan/7236031 to your computer and use it in GitHub Desktop.
Determines odd/even/zeros in a numerical value.
import java.util.Scanner;
public class Numbers
{
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
int evens = 0, odds = 0, zeros = 0;
int number = 0;
// Get number as input
System.out.println("Enter a number: \n");
number = sc.nextInt();
// Calculate even/odd/zeros in number
while (number != 0)
{
// Check if number is evenly disivible by 2 and not 10
if (number % 2 == 0 && number % 10 != 0)
{
evens++;
}
// Determine if number is odd
else if (number % 2 != 0)
{
odds++;
}
// Otherwise, iterate zeros
else {
zeros++;
}
// Move over a digit
number /= 10;
}
// Output the results
System.out.println(number + " has " + zeros + " zeros, " + evens + " evens, and " + odds + " odd numbers.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment