Introduction:
The program finds the sum of odd-positioned digits and even-positioned digits of a given number using C#.NET example.
E.g: If you enter number as 23456, so even number are 2,4,6 & Odd numbers are 3,5 and expected output should be
SumOfEven = 2+4+6 =12
SumOfOdd = 3+5 =8
Program:
using System;
namespace DotNetMirror
{
class SumOfOddEvenDigitsOfNumber
{
static void Main()
{
Console.Write("Enter number to find sum of Even/Odd digits: ");
int number = int.Parse(Console.ReadLine());
int orginalNumber = number;
int sumEven = 0; int sumOdd = 0;
int tempNumber = 0;
while (number != 0)
{
tempNumber = number % 10;
if (tempNumber % 2 == 0)
{
sumEven = sumEven + tempNumber;
}
else
{
sumOdd = sumOdd + tempNumber;
}
number = number / 10;
}
Console.WriteLine("Sum of Event numbers in '{0}' is:{1}", orginalNumber, sumEven);
Console.WriteLine("Sum of odd numbers in '{0}' is:{1}", orginalNumber, sumOdd);
Console.ReadKey();
}
}
}
Ouput: