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


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


201454 = 1 + 5 => 6

932462 = 9 + 3 => 12

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

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int n, r, s = 0;

		Console.Write("Enter a number:");
		n = Convert.ToInt32(Console.ReadLine());

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

Output:

Enter a number:23456
Sum of odd digits:8