Computer Science Canada Adding digits in a number |
Author: | uberwalla [ Sat Dec 16, 2006 1:18 am ] | ||
Post subject: | Adding digits in a number | ||
i do no have a big problem but i just wanted to see if there was a way i could make this code better. i made this for an assignment in tech. it is supposed to add the digits of a number together. u was just wondering if there is an easier way to make this? without using all the instr and strint's and stuff ![]() here is my code:
|
Author: | Clayton [ Sat Dec 16, 2006 7:46 am ] |
Post subject: | |
the way you have your function right now, it's not very dynamic. What If I don't want to input just a three digit number? Instead use a for loop to circle through all of the digits and add them, or, dare I say it, use recursion. |
Author: | uberwalla [ Sat Dec 16, 2006 6:38 pm ] |
Post subject: | |
ok thatll probably work. its just that the assignment was mainly for 3 digit number. ill try that. itll prob work better with arrays too im guessing right? ![]() |
Author: | Clayton [ Sat Dec 16, 2006 6:46 pm ] |
Post subject: | |
not necessarily, a function with a for loop would be a better thing here. |
Author: | StealthArcher [ Sat Dec 16, 2006 9:41 pm ] | ||
Post subject: | |||
This seems like it will work nicely, you can get any size number to work.
|
Author: | uberwalla [ Sat Dec 16, 2006 10:22 pm ] |
Post subject: | |
o ic thx man i understand what freakman was getting at ![]() ![]() ![]() |
Author: | ericfourfour [ Sat Dec 16, 2006 10:32 pm ] |
Post subject: | |
uberwalla IntToString is not a very descriptive name. I would name it numString or numChars. Also, variables and methods usually start with a lower case in Turing. Types, classes and constants start with capitals. Refer to wtd's style guide in Turing tutorials for more information. I am also sure there is a more "math" way to do this. |
Author: | uberwalla [ Sat Dec 16, 2006 10:48 pm ] |
Post subject: | |
yea i know ![]() ![]() im kinda lazy. anyways i kinda still like IntToString because to me it tells me its interger to string. but i get where your getting at. i just did what im used to ![]() thx. |