Here is the recursive function in C#.
private void GetDigitalSum(double num, ref double num2) { //digital sum
double sum = 0;
for (int r = 0; r <= num.ToString().Length - 1; r++)
{
sum += double.Parse(num.ToString().Substring(r, 1));
}
if (sum.Equals(1))
{
num2 = -8;
return;
}
else
{
if (sum > 9)
{
GetDigitalSum(sum, ref num2);
}
else
{
num2 = sum;
}
}
}
//Here is calling subroutine
double no = 1;
double no2 = 0;
double sumr = 0;
double sumc = 0;
ArrayList ar = new ArrayList();
while (no < 1000000)
{
Application.DoEvents();
lblCounter.Text = no.ToString();
GetDigitalSum(no, ref no2);
if (no2.Equals(-8))
{
ar.Add(no);
sumr += double.Parse(no.ToString());
}
no += 1;
}
GetDigitalSum(ar.Count, ref no2);
MessageBox.Show(no2.ToString);
Click link to reveal answer
Comments