I'm new to programming with VB.net and was wondering if someone could tell me how to start making a sum of digits calculator. (NOT ASKING FOR A CODE!) Essentially, the user puts numbers into a textbox (for instance, 1235) and once the user presses a button, the sum of digits (1+2+3+5 = 11) should be displayed in a label. I know I must use some type of loop to do this calculator, but i'm not sure which loop.

Any help will be greatly appreciated

# Problem making a sum of digits calculator

Page 1 of 1## 10 Replies - 5652 Views - Last Post: 08 December 2012 - 12:10 PM

##
**Replies To:** Problem making a sum of digits calculator

### #2

## Re: Problem making a sum of digits calculator

Posted 06 December 2012 - 05:47 PM

First, you need to separate each digit. A For Each loop will do the trick.

As you separate each digit, you can add it to a variable.

Once you've done all digits, you can display the result.

I recommend that you place, above your Class statement, Option Strict On. This will help you get the data types right.

Please feel free to come back with questions if you have any problems.

As you separate each digit, you can add it to a variable.

Once you've done all digits, you can display the result.

I recommend that you place, above your Class statement, Option Strict On. This will help you get the data types right.

Please feel free to come back with questions if you have any problems.

### #3

## Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 03:01 AM

How 'using MOD? You can create a loop dividing the number by 10 then adding the remainder in each loop to become the sum.

Here's the pseudocode:

Here's the pseudocode:

NUMBER = "1235" 'This should be integer to prevent decimal place SUM = 0 WHILE (NUMBER>0) SUM = SUM + (NUMBER/10) NUMBER = NUMBER/10 END WHILE

### #4

## Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 03:03 AM

How 'bout using MOD? You can create a loop dividing the number by 10 then adding the remainder in each loop to become the sum.

Here's the pseudocode:

Here's the pseudocode:

NUMBER = "1235" 'This should be integer to prevent decimal place SUM = 0 WHILE (NUMBER>0) SUM = SUM + (NUMBER/10) NUMBER = NUMBER/10 END WHILE

### #5

## Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 04:37 AM

To OP: how would you know if 123 is 1+2+3 or 12+3 or 1+23 or 123 as it is? It would be seriously limited if summation could be done with n < 10 only.

### #6

## Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 09:48 AM

November-06, on 07 December 2012 - 04:03 AM, said:

How 'bout using MOD? You can create a loop dividing the number by 10 then adding the remainder in each loop to become the sum.

Here's the pseudocode:

Here's the pseudocode:

NUMBER = "1235" 'This should be integer to prevent decimal place SUM = 0 WHILE (NUMBER>0) SUM = SUM + (NUMBER/10) NUMBER = NUMBER/10 END WHILE

While it is certainly possible (and perhaps even preferable) to use Mod, your pseudocode does not use it, and will not return the result intended.

1235 should return 11 as the sum of its digits, but your method will result in 137. Plug it in, give it a try.

### #7

## Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 10:28 AM

...and we have Mod operator in VB.NET. Also such solution would involve Integer division operator (\).

### #8

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 05:16 AM

lar3ry, on 07 December 2012 - 09:48 AM, said:

November-06, on 07 December 2012 - 04:03 AM, said:

How 'bout using MOD? You can create a loop dividing the number by 10 then adding the remainder in each loop to become the sum.

Here's the pseudocode:

Here's the pseudocode:

While it is certainly possible (and perhaps even preferable) to use Mod, your pseudocode does not use it, and will not return the result intended.

1235 should return 11 as the sum of its digits, but your method will result in 137. Plug it in, give it a try.

Sorry, It should have been

SUM = SUM + (NUMBER MOD 10)

instead of

SUM = SUM + (NUMBER/10)

### #9

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:45 AM

How about line 5?

### #10

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:58 AM

Page 1 of 1