10 Replies - 2357 Views - Last Post: 08 December 2012 - 12:10 PM Rate Topic: -----

#1 Hazard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 396
  • 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



Was This Post Helpful? 0
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 396
  • 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



Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Problem making a sum of digits calculator

Posted 07 December 2012 - 09:48 AM

View PostNovember-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.
Was This Post Helpful? 1
  • +
  • -

#7 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • 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 (\).
Was This Post Helpful? 0
  • +
  • -

#8 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 396
  • Joined: 04-January 11

Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 05:16 AM

View Postlar3ry, on 07 December 2012 - 09:48 AM, said:

View PostNovember-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) 



instead of

SUM = SUM + (NUMBER/10)


Was This Post Helpful? 0
  • +
  • -

#9 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:45 AM

How about line 5?
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 08:58 AM

View Postlucky3, on 08 December 2012 - 09:45 AM, said:

How about line 5?

Indeed, November-06!

Hint: Try it with 1234, then 1235
Was This Post Helpful? 0
  • +
  • -

#11 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 152
  • Joined: 22-September 09

Re: Problem making a sum of digits calculator

Posted 08 December 2012 - 12:10 PM

You can start from Here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1