CSnathan

Program 3_6

Jan 11th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. //***********************************************************************************
  2. // Nathan Schnitzer
  3. // Program 3_6
  4. // 12/14/16
  5. // This program will read an integer and print the number of odd, even and digits in that number
  6. //***********************************************************************************
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class Prog3_6 {
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scan = new Scanner(System.in);
  14.         int value;
  15.         double zero_mult = 10.0;
  16.         double value_compare;
  17.         int even_count = 0, odd_count = 0, zero_count = 0;
  18.        
  19.         //Prompt for a integer value
  20.         System.out.println("Please enter a value : ");
  21.         value = scan.nextInt();
  22.        
  23.         value_compare = value;
  24.         int zero_check = (int)(value_compare / zero_mult);
  25.        
  26.         if (value_compare / zero_mult == zero_check)
  27.         {
  28.             zero_count++;
  29.         }
  30.         else if (value_compare % 2 == 0)
  31.             even_count++;
  32.         else if (value_compare % 2 == 1)
  33.             odd_count++;
  34.        
  35.         value_compare = (int)(value_compare / zero_mult);
  36.        
  37.         while (value_compare >= 1) //Will go from the second digit
  38.         {
  39.             if (value_compare / zero_mult == zero_check)
  40.             {
  41.                 zero_count++;
  42.                 System.out.println("zero");
  43.             }
  44.             else if ((int)value_compare % 2 == 1) //odd
  45.             {
  46.                 odd_count++;
  47.                 System.out.println("odd");
  48.             }
  49.             else if ((int)value_compare % 2 == 0)
  50.             {
  51.                 even_count++;
  52.                 System.out.println("even");
  53.             }
  54.                    
  55.                 value_compare = (int)(value_compare / zero_mult);
  56.         }
  57.        
  58.  
  59.  
  60.        
  61.         //Print the results
  62.         System.out.println();
  63.         System.out.println(value + " contains:");
  64.         System.out.println();
  65.         System.out.println(zero_count + " zeroes");
  66.         System.out.println(odd_count + " odd digits");
  67.         System.out.println(even_count + " even digits");
  68.  
  69.         scan.close();
  70.        
  71.     }
  72.  
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment