-8

I'm trying to code a program where it shows all the possible two digits numbers and shows the sum of those digits.

for example:

10 - 1 + 0 = 1
11 - 1 + 1 = 2
12 - 1 + 2 = 3
..
98 - 9 + 8 = 17
99 - 9 + 9 = 18

I used while loop to get the positive numbers like this:

public static void main(String[] args){
   int count = 10;

   while (count < 100) { 
      System.out.println("count:" + count); 
      count = count + 1;    
   }

   System.out.print("Sum of Digits of " +sum);      
}

I'm stuck with getting the sum of these digits like in the example. I read some stuffs on Internet with no luck.

Angel Cuenca
  • 1,637
  • 6
  • 24
  • 46
GaryM
  • 11
  • 1
  • 5
  • You told us the desired output but not the output you're getting. Are you getting errors? Incorrect output? etc – jhhoff02 May 24 '17 at 13:38
  • Try breaking the problem down into smaller problems. In each of those equations you need three numbers. The first one you already have (the counter). Now think about how to calculate the other two numbers based on the counter. – Rolf Schäuble May 24 '17 at 13:41
  • 1
    Getting the individual digits shouldn't be a problem, especially since you know the maximum size of your numbers (i.e. 2 digits max): try `sum = count/10 + count %10;` – Thomas May 24 '17 at 13:41
  • @jhhoff02 - Uhm, no. While loop works fine and it shows all the positive two digits numbers like it should. I can't seem to get the sum of them like in the example. sum = count % 10; maybe something like that would work? That's the part where I got stuck. – GaryM May 24 '17 at 13:43
  • The units digit you can obtain by count%10 and the tens digit by count/10. So it should be something like this: System.out.println("Sum of Digits of " + count + " is " + count/10 + " + " + count%10 + " = " + count/10+count%10 + "."); – Veliko May 24 '17 at 13:44
  • I really don't understand why there are so many negative votes... – Veliko May 24 '17 at 13:45
  • @Veliko - Thank you Veliko. Helped alot. and I just saw those negative votes. oh well :3 – GaryM May 24 '17 at 13:50
  • try to use `int count = 10; while (count < 100) { int result = count / 10 + count % 10; System.out.println(count + " - " + (count/10) + " + " + (count % 10) + " = " + result); count++; }` – Youcef LAIDANI May 24 '17 at 13:50
  • I'm surprised nobody's considered converting it to a string and summing the values of individual characters. It's easier to explain. – byxor May 24 '17 at 14:13
  • @byxor it's not really easier since you need to parse the characters into numbers again and more code would be required. If the length of the number/amount of digits is unknown that's definitely an option. – Thomas May 24 '17 at 15:13
  • On the downvotes: I'd have to guess but I'd assume it's due to you not showing what you've tried so far (read [ask] for a reference) but only stating "I read some stuffs on Internet with no luck." - people might see that as lack of effort. – Thomas May 24 '17 at 15:15

2 Answers2

0

A solution which works, if you really only have two digits per number:

int count = 10;
while (count <100){
    int first = count /10;
    int second = count %10;
    System.out.println(count + " - " + first +" + "+ second + " = " + (first+second));
    count = count + 1;
}

When learning Java, you should, as first step, learn about the basic operators, like / and %. These two are needed for this quiet simple example.

I tried to stay as close to your code as possible to get the expected result. However, There are other solutions (for-loop, count++ instead of count = count + 1 ...)

Thomas Böhm
  • 1,456
  • 1
  • 15
  • 27
0

An easy way to get the sum of digits for n-digit number is this:

int sum = 0;
while (num > 0) {
    int digit = num % 10;
    sum = sum + digit;
    num = num / 10;
}

num % 10 will give the remainder of the division between num and 10 which is the last digit of num. You add that to the sum variable.
num / 10 will give the result of division between num and 10. Because both num and 10 are integers, the result will also be an integer. Dividing by 10 will just shift the decimal point left one place and since the result is an integer, the digit left after the decimal point will be removed.

Example:
num = 108
num % 10 = 108 % 10 = 8
num / 10 = 108 / 10 = 10.8 -> 10

This continues until num becomes a single digit. When num is a single digit, division by 10 will return 0.

Example:
num = 5
num % 10 = 5 % 10 = 5
num / 10 = 5 / 10 = 0.5 -> 0

This last digit will be added to the sum and the loop will stop.

Loading BG
  • 131
  • 2
  • 9