1
Answer

Find the sum of alternate digits in a number in C#

Find the sum of alternate digits in a number and also please view the following code and help me with using while loop and also using string type and splitting the string and storing in an array using array index
Example:
If number is 125689
sum1=1+5+8=14; and sum2=2+6+9=17 
 
 
namespace Prime2
{
class SumOfEvenAndOddDigits
{
public static void ConvertToArrayOfDigits(int value, int[] digits)
{  		for (int index = digits.Length - 1; index >= 0; index--)
 {  			digits[index] = value % 10;
     			value = value / 10;
 } }
static void Main(string[] args)
{
double number,num1,res,rem;
//string number;
double temp = 0;
//double ar[15];
Console.WriteLine("Enter any Number");
number = double.Parse (Console.ReadLine());
num1 = number;
while (number>0)
{
res = number % 10;
temp = temp + res;
number = number % 100;
}
Console.WriteLine("Even Digit Sum is " + temp);
while (num1>0)
{
rem = num1 % 10;
rem = rem / 10;
temp = temp + rem;
num1 = num1 / 100;
}
Console.WriteLine("Odd Digit Sum is " + temp);
}
}
}
 
Answers (1)