# Problem making a sum of digits calculator

Page 1 of 1

## 10 Replies - 3459 Views - Last Post: 08 December 2012 - 12:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303040&amp;s=416afe3d0c11c6d19b78f87e549e4a4e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hazard

Reputation: 0
• Posts: 4
• Joined: 06-November 12

# Problem making a sum of digits calculator

Posted 06 December 2012 - 04:50 PM

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

Is This A Good Question/Topic? 0

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

### #2 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

## 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.

### #3 November-06

• D.I.C Regular

Reputation: 46
• Posts: 437
• Joined: 04-January 11

## 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:

```
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 November-06

• D.I.C Regular

Reputation: 46
• Posts: 437
• Joined: 04-January 11

## 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:

```
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 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## 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 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

## 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:
```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 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## 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 November-06

• D.I.C Regular

Reputation: 46
• Posts: 437
• Joined: 04-January 11

## 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:
```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.

Sorry, It should have been

```SUM = SUM + (NUMBER MOD 10)

```

```SUM = SUM + (NUMBER/10)

```

### #9 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:45 AM

### #10 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,294
• Joined: 12-September 12

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:58 AM

lucky3, on 08 December 2012 - 09:45 AM, said:

Indeed, November-06!

Hint: Try it with 1234, then 1235

### #11 ebolisa

Reputation: 7
• Posts: 160
• Joined: 22-September 09

## Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 12:10 PM

You can start from Here