## 3 replies

Last post Sep 20, 2016 06:07 AM by Deepak Panchal

• shmail

Member

42 Points

116 Posts

### split integer and total each digit number

how to split integer and total each digit number in c#

for example

mynumber: 5412

sumnumber: 1

result: 6523

• mgebhard

All-Star

31341 Points

11552 Posts

### Re: split integer and total each digit number

Looks like homework...

```static void Main(string[] args)
{

int num = 5412;
int sumNumber = 2;
int tens = 0;

while (num / (sumNumber) > 1)
{
tens += sumNumber;
sumNumber = sumNumber * 10;
}

Console.Write(num);
Console.Write(" + ");
Console.Write(tens);
Console.Write(" = ");
Console.WriteLine(num + tens);
}```

• shmail

Member

42 Points

116 Posts

### Re: split integer and total each digit number

thanks mgebhard

my project is total two digit of number and result total with next digit.

for example:

my number: 523167

i want

5+2=7

7+3=10

10+1=11

11+6=17

17+7=24/255

or use ^ XOR method

do you have any solution?

• Deepak Panch...

Contributor

2680 Points

1122 Posts

### Re: split integer and total each digit number

Sep 20, 2016 06:07 AM|Deepak Panchal|LINK

Hi Shmail,

use the code mentioned below.

```   private void button1_Click(object sender, EventArgs e)
{
int num, result;

pro pg = new pro();

num = int.Parse(textBox1.Text);

result = pg.sum(num);
label1.Text = result.ToString();
}

}
class pro

{

public int sum(int num)

{

if (num != 0)

{

return (num % 10 + sum(num / 10));

}

else

{

return 0;

}

}

}```

it will give you output like below as per your requirement.

Regards

Deepak

MSDN Community Support