如何输入2位或更多位的整数然后产生整数的总和? [英] How do I input integer with 2 or more digits then produce a sum for the whole number?
本文介绍了如何输入2位或更多位的整数然后产生整数的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在c#中创建一个程序,当插入一个字符串/整数时,它会添加。
例如:
插入:61输出:6 + 1 = 7
插入:234输出:2 + 3 + 4 = 9
我不知道如何对此进行编码,但我认为输入的数字应该存储在一个数组中然后它会在一个循环中调用它们来添加?我不太确定!请帮忙。谢谢!
Hi, I need to create a program in c# that when inserted a string/integer it will add.
E.g:
Insert: 61 Output: 6+1=7
Insert: 234 Output: 2+3+4=9
I don't know how to code this, but I was thinking that the entered number should be stored in an array then it will called in a loop for them to add? I'm not really sure! Please help. Thanks!
推荐答案
回答 - C#中的数字总和 [ ^ ]。
Answer at - Sum of digits in C#[^].
sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
这篇关于如何输入2位或更多位的整数然后产生整数的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文