C# function that takes an integer as a parameter and adds the alternate digits in the integer

A2H
Posted by A2H under C# category on | Points: 40 | Views : 648
public  int sumDigits(int inputValue)
{
//Varibale to hold the sum
int TotalSum = 0;
//Convert your integer to array of digits
int[] aryDigits = Array.ConvertAll(inputValue.ToString().ToArray(), x => (int)x - 48);
//Loop through the array take the alternative numbers
for (int i = 0; i < aryDigits.Length; i = i + 2)
{
//Add the alternative numbers and then assign it to variable
TotalSum = TotalSum + Convert.ToInt32(aryDigits[i]);

}
//Return the total value
return TotalSum;
}

Comments or Responses

Login to post response