0

I need help writing a Java program that prompts a user to enter a number than adds each digit without using if or while statements.

For example: I enter 987. The program should say this number has 3 digits, and multiply each digit to it's decimal place.

9*100
8*10
7*1

This is my code so far, I think I am on the right path,but don't know where to go from here.

public static void main(String[] args) {
   Scanner keyboard = new Scanner(System.in);
   System.out.print("Please enter number");
   int x;

  x= keyboard.nextInt();
  for(int i=1;i<=20;i++){
      System.out.print(x/1);
      System.out.print(x/10);
      System.out.print(x/100);
      System.out.print(x/1000);
      System.out.print(x/10000);
      System.out.print(x/100000);
  }
}

}

skaffman
  • 398,947
  • 96
  • 818
  • 769
Cooldude
  • 63
  • 1
  • 1
  • 7
  • 1
    Get the input as a `String`, this gives an idea of length. Use `String#toCharArray` or `String#charAt` to get the next character in the `String`, convert this to an `int`, perform you multiplication on it – MadProgrammer Mar 30 '15 at 01:04
  • We haven't learned Char yet, so I can't use that. – Cooldude Mar 30 '15 at 01:10
  • It seems like the challenge here is not to actually do what is requested, but to what is requested *under the arbitrary restrictions of the question*, such as "no if or while statements". If you respond to each offer of assistance with "oh we can't use that, because we haven't learned it yet", people will become disinterested very quickly. – Greg Hewgill Mar 30 '15 at 01:16
  • My bad that's not my intentions, I am stuck on this problem for hours and need help. – Cooldude Mar 30 '15 at 01:18
  • 1
    You've learnt `String` and `int`, `char` is just another primitive type like `int` – MadProgrammer Mar 30 '15 at 01:19
  • Char is probably easy to learn and I can probably spend an hour and learn it with ease, but for this I can't use char. – Cooldude Mar 30 '15 at 01:21
  • So what you're saying is that it is strictly required to accept the input as an integer and it must remain integer(s) through the entire process; right? What is the loop for; are you supposed to be continually taking input or outputting the same results? – ChiefTwoPencils Mar 30 '15 at 01:24
  • For this homework I can use loops, int, math operations, strings, – Cooldude Mar 30 '15 at 01:35
  • Thanks it helped a lot, I got the part where you can divide the number by 10, but how would java know the number I enterd has 3 digits. – Cooldude Mar 30 '15 at 02:07

2 Answers2

0

You can do this to extract each digit:

  System.out.println((x%100000) / 10000);
  System.out.println((x%10000) / 1000);
  System.out.println((x%1000) / 100);
  System.out.println((x%100) / 10);
  System.out.println(x%10);

So if you input 567, the output will be:

0
0
5
6
7

What you can do from there is have a int total and multiply each one and add it to the total.

To get the int size, check out this link:

https://stackoverflow.com/a/22649027/3735292

Community
  • 1
  • 1
Tman
  • 136
  • 8
  • Thanks this helped, but I also need to make it so if I enter 567 the program should say there are 3 digits in this number. – Cooldude Mar 30 '15 at 02:24
  • Check this out. It doesn't use if or while loops: http://stackoverflow.com/a/25467011/3735292 – Tman Mar 30 '15 at 02:27
  • Thanks that helped, also how would I remove the 0s. So If I type 567 it should come as 5,6,7 seperately and there shouldn't be 0s. – Cooldude Mar 30 '15 at 02:52
  • Count the number of digits in the input: So 567 would be 3. Save that value (3) into a variable. In the for loop where the x%10 stuff is happening, change i<=20 to i <= digitCount. That way it will only run through three times. Also, you may need to change the order of stuff printing from the way I originally had it. Switch x%10 to the top, etc... – Tman Mar 30 '15 at 02:59
  • Not a problem! Good luck :) – Tman Mar 30 '15 at 03:30
0

Instead of using the large division you can use mod and division to get each digit out in a for loop like this:

public static void main(String[] args) {
   Scanner keyboard = new Scanner(System.in);
   System.out.print("Please enter number");
   int x;    
   int a=0, result=1;
       for(int i = keyboard.nextInt(); i!=0; a=i%10, i/=10)
            result*=a;
   System.out.println(result);
}
Nathan
  • 38
  • 5
  • According to the question, this is supposed to be done "without using if [or] while statements". – Nic Mar 30 '15 at 02:32