# Sum of Digits in C

Page 1 of 1

## 11 Replies - 2987 Views - Last Post: 13 February 2010 - 11:39 AMRate 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=76211&amp;s=1be231233ea9220088a56ee22bcc8e09&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 MagiCarpet

Reputation: 0
• Posts: 11
• Joined: 09-December 08

# Sum of Digits in C

Posted 11 December 2008 - 08:34 AM

Hey guys, I was just wondering: If I wanted to ask a user for an integer input and then find the sum of the digits that were entered, would there be any way of going about doing that?
Is This A Good Question/Topic? 0

## Replies To: Sum of Digits in C

### #2 Quazzle

Reputation: 0
• Posts: 15
• Joined: 11-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 08:39 AM

I'm a bit confused. are u asking for the program where the input is something like "23" and the output is "2+3" or the program where the input is "2,3" and the output is "2+3" ? both are easy enough. plz just tell me which one ur looking for.

### #3 MagiCarpet

Reputation: 0
• Posts: 11
• Joined: 09-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 08:41 AM

Yeah, like if the user put in 44, then there would be a message that says "The sum of the digits is 8." ....something like that.

### #4 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Sum of Digits in C

Posted 11 December 2008 - 08:48 AM

```string response;
int total = 0;
cout << "Enter a number:";
cin >> response;
for(int  i = 0; i < response.size(); i++)
{
total += (int)response.at(i);
}

```

I haven't ran it though, the typecast may or may not be necessary.

### #5 Quazzle

Reputation: 0
• Posts: 15
• Joined: 11-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 08:54 AM

ha! KYA beat me to it. well thanks anyway KYA. I think mine had a small mistake.

### #6 MagiCarpet

Reputation: 0
• Posts: 11
• Joined: 09-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 08:55 AM

Actually, it's working beautifully on this end the way it is. Thank you very much.

• Saucy!

Reputation: 6207
• Posts: 23,951
• Joined: 23-August 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 09:21 AM

Except that's C++, not C.

### #8 KYA

• yay verily

Reputation: 3155
• Posts: 19,198
• Joined: 14-September 07

## Re: Sum of Digits in C

Posted 11 December 2008 - 09:58 AM

You could do the same thing with a char array.

I need to L2R though

• Saucy!

Reputation: 6207
• Posts: 23,951
• Joined: 23-August 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 10:00 AM

Indeed you could, with only a couple of changes. I just wanted the OP to be aware, in the event the assignment was C-specific.

### #10 MagiCarpet

Reputation: 0
• Posts: 11
• Joined: 09-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 10:00 AM

Yeah thats ok...I converted to to C no problem.

### #11 cguru

Reputation: 0
• Posts: 1
• Joined: 11-December 08

## Re: Sum of Digits in C

Posted 11 December 2008 - 10:08 PM

Well magiCarpet.i believe u should make use of '%' and '/' along with bitwise operators to shorten your code.

below is a code to calculate sum of digits :
```#include<stdio.h>
#include<conio.h>
int main()
{
int num=0,tot=0;
printf("Enter the number:");
scanf("%d",&num);
while(num)
{
tot = tot + (num%10);
num=num/10;
}
printf("The sum of Number is %d\n",tot);
getch();
return 0;
}
```
Hope this is useful to u..

This post has been edited by cguru: 11 December 2008 - 10:11 PM

Reputation:

## Re: Sum of Digits in C

Posted 13 February 2010 - 11:39 AM

thank u