# Take an integer and return the digits added together?

Page 1 of 1

## 7 Replies - 427 Views - Last Post: 29 October 2013 - 02:50 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=332674&amp;s=f530b38f3d626872e08812886c397a28&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ProgramKid

Reputation: 0
• Posts: 22
• Joined: 28-October 13

# Take an integer and return the digits added together?

Posted 28 October 2013 - 11:19 AM

Totally lost on how to do this. Can someone please explain how to do this code? or give an example close to it?

Full Q:
You will write a function named countDigit that will take an integer and returns the digits added together. For instance, the number 123 will return 6 (1 + 2 + 3).
Is This A Good Question/Topic? 0

## Replies To: Take an integer and return the digits added together?

### #2 modi123_1

• Suitor #2

Reputation: 10029
• Posts: 38,310
• Joined: 12-June 08

## Re: Take an integer and return the digits added together?

Posted 28 October 2013 - 11:27 AM

Convert the integer into a string.. a string is just an array of characters, right? Use that with a loop to visit each character in the array.. add'em together!

### #3 ProgramKid

Reputation: 0
• Posts: 22
• Joined: 28-October 13

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 12:57 PM

help. can't figure it out. Trying to take an integer and returns the digits added together. For instance, the number 123 will return 6 (1 + 2 + 3).
but for some reason I keep getting like 212 for 12.

```def countDigits():
loop = str(input("Enter digits you want to be added:"))

for ctr in loop:
int(loop)
ctr += loop
print(ctr)

countDigits()

```

### #4 modi123_1

• Suitor #2

Reputation: 10029
• Posts: 38,310
• Joined: 12-June 08

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 01:01 PM

Please do not open duplicate topics.

Merging.

### #5 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:32 PM

modi123_1, on 28 October 2013 - 01:27 PM, said:

Convert the integer into a string.. a string is just an array of characters, right? Use that with a loop to visit each character in the array.. add'em together!

In most languages, this is actually not going to work. The character '1' is not the integer 1, so an additional step is required. I think it might work in PHP, but few languages are so type-irresponsible.

ProgramKid, you're actually pretty close. The problem you're having now is that the int() function doesn't change its argument, it returns a value. So contemplate this for a minute and see if it helps:

```>>> foo = '123'
>>> int(foo)
123
>>> type (foo)
<type 'str'>
>>> bar = int(foo)
>>> type (bar)
<type 'int'>
>>>
```

### #6 modi123_1

• Suitor #2

Reputation: 10029
• Posts: 38,310
• Joined: 12-June 08

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:36 PM

Well yeah.. I figured type casting was sort of *hand waving* implied.. or something.

### #7 jon.kiparsky

• Pancakes!

Reputation: 8385
• Posts: 14,448
• Joined: 19-March 11

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:41 PM

I think that's a big assumption when we're dealing with a sum-the-integers problem...

### #8 modi123_1

• Suitor #2

Reputation: 10029
• Posts: 38,310
• Joined: 12-June 08

## Re: Take an integer and return the digits added together?

Posted 29 October 2013 - 02:50 PM

Tou-freaking-che'