I want to write a program to add all the digits of the entered number. For example, when I enter 54496 the output must be 5 + 4 + 4 + 9 + 6 = 28.
+6
A:
This is homework, so this is just a (quick maybe buggy) hint.
1) Initialize sum in 0;
sum := 0
2) take the reminder of the division by 10 of the number and add it to sum.
`sum:= sum + number mod 10`
3) update number to the result number divided by 10.
`number := number div 10`
4) if number > 1 goto 2
`if (number greater than 1) goto 1`
sum:= sum + number
Tom
2010-08-22 08:09:13
A:
#include <iostream>
using namespace std;
int main(){
int a;
cin>>a;
int sum=0;
while (a!=0){
sum+= a %10;
a/=10;
}
cout<<"sum is : "<<sum<<endl;
return 0;
}
-1 You didn't help him. You actually encouraged a very bad attitude for a future programmer.
Andrei Ciobanu
2010-08-22 08:54:42
-1 You name yourself "c-programmer" but you'r code is written in C++. Weird.
Helper Method
2010-08-22 09:13:45
i wanted to write c++programer but it wrote c-programmer so not surprise it is registartion problem
2010-08-22 09:19:41
-1 for posting source in response to homework. I wish I could give you another -1 for posting C++ code in response to C question,.
qrdl
2010-08-22 11:28:49
i helped him by the way i dont think that helping of people is very bad for their even in future and also c++ code i can change it to c code it is not problem just i wanted show for him how to solve such problems languages is not important
2010-08-22 11:54:00
@c-programmer OP will learn nothing from blindly copying your code. For homework questions you should give a clue, not the code. BTW was it you who have just downvoted both my questions in retaliation?
qrdl
2010-08-22 12:10:37
@qrdl please dont want downvote ok?maybe not study but i helped please upvote and dont say i wish could give you another -1
2010-08-22 12:20:19
make sume change in your post and i wil upvote now i can't do it because it does not give me possibility to do it
2010-08-22 12:21:51
@c-programmer I've explained why I've downvoted your answer, unlike your downvotes to my questions. And and tell me what I should or shouldn't do.
qrdl
2010-08-22 12:23:56
@qrdl look at the second saying I wish I could give you another -1 for posting C++ code in response to C question,. what do you think is this rude say?
2010-08-22 12:35:56
@c-programmer: if you don't want to lose reputation from downvotes, don't post in a way that invites downvotes. If you want to help someone with their homework, help them learn. Pasting code in a language that isn't even what an asker is looking for is not the way to answer questions on this site, especially when it comes to homework questions. In fact, all you did is paste code. There aren't even any comments or an English explanation of it. How do you expect OP to understand all this stuff if you don't even explain it? (Also, I did not downvote.)
BoltClock
2010-08-22 14:17:46
@c-programmer @qrdl: referring to "another -1 for posting C++ code in response to C question", imagine you ask a question in English, and somebody answers you in French instead of English, but you don't know French at all. How would you feel? (Don't tell me about Google Translate, that is not relevant.)
BoltClock
2010-08-22 14:23:29
-1 for all the reasons stated.
mathepic
2010-08-22 16:11:58