QUOTE(FeedBacker @ Feb 24 2008, 12:21 AM)

write a C# program to find out the sum of odd & even digits in a given large integer. for Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.
Decrementing Number Of Digits Ofter Point
I need a complete C# program of
Write a C# program to find out the sum of odd & even digits in a given large integer. For Ex:- input= 147936 and output:- odd total-1+7+3=11 & even total-4+9+6=19.
Can any one of you give me this program?
-reply by malleswararao
Thats easy enough. The trick is in converting the number to string and then taking out one character at a time using the string type's substring method in a loop. Use the counter variable to check if the current position is odd or even and correspondingly add the digit to the running sum of Even or Odd numbers. Given below is the code.
CODE
using System;
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number:");
int Num = Convert.ToInt32(Console.ReadLine());
string NumInString = Num.ToString();
int OddSum = 0, EvenSum = 0;
for (int i = 0; i < NumInString.Length; i++)
{
int Digit = Convert.ToInt32(NumInString.Substring(i, 1));
if (i % 2 != 0)
EvenSum += Digit;
else
OddSum += Digit;
}
Console.WriteLine("Odd Sum = {0} Even Sum = {1}", OddSum, EvenSum);
Console.Read();
}
}
Comment/Reply (w/o sign-up)