Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C# How to do Sum of Digits for very big number in a textbox 4534534534543533453432432434324324324234 and result display in other text box as 140 / 14 / 5. if number is small ex. 45345345343 result text box display as 43 / 7. Even smaller ex.45 display as 9. Please any body help it is urgent for my project.
It is not for my school/college project ... sorry for bad English...
Code done so far...

C#
 public long DigitSum(String BigNumberStr,int len)
    {
        int total = 0;       
        int s=BigNumberStr.Length;

        if (len == 10)
        {
            for (int i = 0; i < s; i++)
                total += int.Parse(Convert.ToString(BigNumberStr[i]));
        }
        else if (len == 100)
        {
            for (int i = 0; i < s; i++)
                total += int.Parse(Convert.ToString(BigNumberStr[i]));
        }
            
                return total;
 }
fun.DigitSum(TextBox2.Text + TextBox10.Text, 100);
fun.DigitSum(TextBox2.Text + TextBox10.Text, 1000);



But it's not working please help somebody... Result display in following format..
2132 = 8
213245 = 17 / 8
21324545454454545365756756 = 116 / 17 / 8
Posted
Updated 2-Sep-14 0:06am
v5
Comments
[no name] 1-Sep-14 14:54pm    
"I want" is not a question or a problem. Just because you waited until the day before your homework assignment was due, does not make it urgent for us. It's not urgent at all. If it were any kind of urgent, you would have shown us what effort you have made and described an actual problem with the code that you have written.
Sergey Alexandrovich Kryukov 1-Sep-14 15:09pm    
Homework? Not a question. Not acceptable as question. Not urgent. Just the abuse.
—SA
George Jonsson 2-Sep-14 5:35am    
The code above cannot even compile as your parameter list in the method call doesn't match the method declaration.
public long DigitSum(String BigNumberStr) : 1 parameter of type string
fun.DigitSum(TextBox2.Text + TextBox10.Text, 100); : 2 parameters, string and int
So what is not working for you?

Well, it looks a pretty trivial task, a simple loop over the string characters would do the trick. Just reproduce the algorithm you would use to perform the same task with pencil and paper.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Sep-14 23:01pm    
5ed.
—SA
CPallini 2-Sep-14 1:44am    
Thank you.
Matt T Heffron 2-Sep-14 13:34pm    
+5
CPallini 2-Sep-14 13:56pm    
Thank you.
You're thinking about wrong. It's not a "very big number". It's a string of characters that just happen to be digits.

So, enumerate the characters in the string and get the integer value of each character and add them up.

No, I'm not writing the code for you because this is an obvious homework assignment. You've already been given everything you need to write this in your class.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Sep-14 23:00pm    
Best answer so far, a 5.
—SA
amith9 2-Sep-14 5:12am    
Thanks...i have done something with your guidelines but it's not working properly...
Sergey Alexandrovich Kryukov 2-Sep-14 11:29am    
Well, it means your "something" is wrong. You can show your code; and we can see where is your bug...
—SA
Matt T Heffron 2-Sep-14 13:35pm    
+5
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

Hint: Start with the BigInteger structure[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Sep-14 23:03pm    
Even though BigInteger is amazingly good type, it is totally unrelated to this home assignment. Strictly speaking, in certain sense, the input is not really a "number"; this is already a string which is supposed to contain only the digits. It makes the problem well too trivial, of course.
—SA
try below code


C#
string sum = TB1.Text;
       do
       {
           int val = 0;
           for (int i = 0; i < sum.Length; i++)
           {
               val += int.Parse(sum[i].ToString());
           }
           sum = val.ToString();
       }
       while (sum.Length > 1);
       Label1.Text = sum;
 
Share this answer
 
v3
Comments
amith9 2-Sep-14 10:22am    
Please correct the following code..
Hello ClimerChinna, thank u very much for your help, it is working. But how to find sum of digits from two textboxes, the following code properly not working..Thanks in advance



TextBox10.Text = Convert.ToString(fun.DigitSum(TextBox2.Text + TextBox10.Text, 2)) + "/" + Convert.ToString(fun.DigitSum(TextBox2.Text + TextBox10.Text, 1));

public string DigitSum(String BigNumberStr,int len)
{
while (BigNumberStr.Length > len)
{
int val = 0;
for (int i = 0; i < BigNumberStr.Length; i++)
{
val += int.Parse(BigNumberStr[i].ToString());
}
BigNumberStr = val.ToString();
}

return BigNumberStr;
}
ClimerChinna 3-Sep-14 0:32am    
i could not understand your problem, sum of digits from 2 textboxes means
1) you want to add the numbers then calculate sum of digits
2) You want to concatenate both numbers then calculate sum of digits

tell me what you wanted to do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900