0

I need to write a program that accepts the following conditions:

  1. It has to be an odd number,
  2. Have an odd number of digits,
  3. All of its digits have to be odd numbers and
  4. The number has to be between 101 and 1000001.

I am currently stuck on trying to check if it has an odd number of digits. Any help would be appreciated. Update: Thanks for all the help everyone. It was really helpful!

import java.util.Scanner;
public class TestOddNumbers {

public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);
    int userInput;
    final int EXIT = -1;
    final int MIN = 101;
    final int MAX = 1000001;
    do{
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();      
    }   
    while(userInput != EXIT && userInput >= MIN && userInput <= MAX); 
    if(userInput % 2 == 1){  
          System.out.println("This is an odd number");
        } 
        else{
            System.out.println("This is not an odd number");
        }
      }  
   }        
}
Dekart Kosa
  • 39
  • 2
  • 9
  • 2
    The first rule is redundant. – shmosel Jan 27 '17 at 06:53
  • 4
    Possible duplicate of [Way to get number of digits in an int?](http://stackoverflow.com/questions/1306727/way-to-get-number-of-digits-in-an-int) – shmosel Jan 27 '17 at 06:54
  • if *all of its digits have to be odd numbers* then as the last digits will be odd then the number will be odd – Scary Wombat Jan 27 '17 at 06:55
  • 1
    convert to a String and then split into chars – Scary Wombat Jan 27 '17 at 06:56
  • Convert the number to a String using `Integer.toString` and check if it's length is odd. – Trinopoty Jan 27 '17 at 06:56
  • @ScaryWombat No, 5555 have an even number of digits but all digits are odd – Sweeper Jan 27 '17 at 06:57
  • @Sweeper *5555* - all of its digits have to be odd numbers **is what I answered to** – Scary Wombat Jan 27 '17 at 06:58
  • if you want to know the number of digits. put the number into a temp var. Then divide that number by 10, until the value of the division = 0. Then the number of times you did the division including when the division = 0 is the number of digits. –  Jan 27 '17 at 07:02
  • @holycatcrusher why not put the number into a temp String var and just ask for its length? As each char/digit needs to be examined anyway – Scary Wombat Jan 27 '17 at 07:04
  • @Scary Wombat, I wouldn't put it into a string, because it is less efficient. It would use more memory, and more computing power. It wouldn't be a problem here, but in a algorithm that analyzes lot's of data it would make a big difference. –  Jan 29 '17 at 07:07

5 Answers5

0

How about this ?

This psuedocode is not a solution or an attempt to a solution.

//this should probably be outside and before your while loop.
int length = Integer.toString(userInput).length();
if (length % 2 == 0) {
  return false; //even number of digits
}

...

//this should be inside your while loop
while(userInput != 0) {
  remainder = userInput % 10;
  userInput = userInput / 10;
  if (remainder % 2 != 0) {
    return false; //One of the digit is even
  }
  //if every digit is odd, it has to be an odd number, so checking if origina userInput is an odd number is not necessary. 
}

Take this pseudo-code as the starting point.

0

try passing the value of userInput into a string and check its length if it is odd or not.

String total = "";

do{
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();
        total += String.valueOf(userInput);
    }   
    while(userInput != EXIT && userInput >= MIN && userInput <= MAX);
    System.out.println(total);
    if(total.length() % 2 == 1){  
          System.out.println("This is an odd number");
        } 
        else{
            System.out.println("This is not an odd number");
        }
dranreb dino
  • 260
  • 2
  • 17
0

This piece of code will check if each digit is odd and also if length is also odd.

    int test = 6789;
    int a = test;
    int length =0;
    boolean hasEachDigitOdd = true;
    while (a!= 0) {
        int remaider = a % 10;
        if (remaider % 2 == 0) {
            hasEachDigitOdd = false;
        }
        a = a / 10;
        length++;
    }
    System.out.println(length);
    System.out.println(hasEachDigitOdd);
userJ
  • 181
  • 1
  • 4
0
if (num >= 101 && num <= 1000001 && num % 2 == 0)

The % sign gives you the remainder of a division operation.

Ben Arnao
  • 492
  • 5
  • 11
-1

Please check below - the solution that meets all your needs. Even the way you took input needed to be fixed.

public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int userInput;
    final int EXIT = -1;
    final int MIN = 101;
    final int MAX = 1000001;
    do {
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();

        if(userInput==EXIT){
            System.out.println("done");
            break;
        }

        if (userInput % 2 == 1) {
            System.out.println("This is an odd number");
        } else {
            System.out.println("This is not an odd number");
        }

        if(String.valueOf(userInput).length()%2==1){
            System.out.println("Has odd number of digits");
        } else {
            System.out.println("Has even number of digits");
        }
        boolean[] allOdds = {true};

        String.valueOf(userInput).chars().forEach(i -> {
            if(Integer.parseInt(String.valueOf((char)i))%2==0){
                allOdds[0] = false;
            }
        });
        if(allOdds[0]){
            System.out.println("all digits are odds");
        }else{
            System.out.println("all digits are not odds");
        }

    } while (userInput >= MIN && userInput <= MAX);
}
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45