C# Program to find the sum of even and odd digits of a number


In this program, You will learn how to find the sum of even and odd digits of a number in C#.


12345 = 2 + 4 => 6

12345 = 1 + 3 + 5 => 9

Example: How to find the sum of even and odd digits of a number in C#

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int n, r, se = 0, sod = 0;
		Console.Write("Enter a number:");
		n = Convert.ToInt32(Console.ReadLine());

		while (n > 0)
		{
			r = n % 10;
			if (r % 2 == 0)
			{
				se = se + r;
			}
			else
			{
				sod = sod + r;
			}
			n = n / 10;
		}
		Console.WriteLine("Sum of even digits:" + se);
		Console.WriteLine("Sum of odd digits:" + sod);
	}
}

Output:

Enter a number:3456
Sum of even digits:10
Sum of odd digits:8