C# program to find the sum of odd digits of the product of two numbers.

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 268
Let us say we have two numbers num1 = 19 and num2 = 65. The product of num1 and num 2 is 1235.
The odd numbers are 1,3 and 5 and their sum is 9. The below program will do so



using System;
using System.Linq;
using System.Numerics;

namespace ConsoleApplication2
{
class Program
{

static void Main(string[] args)
{

BigInteger num1 = 19;
BigInteger num2 = 65;

//Approach 1
Console.WriteLine("The sum of odd digits of the product of {0} and {1} is {2}", num1, num2,
(num1 * num2)
.ToString()
.ToCharArray()
.Where(w => Convert.ToInt32(w) % 2 != 0)
.Select(c => c - 48)
.Sum());

//Approach 2
Console.WriteLine("The sum of odd digits of the product of {0} and {1} is {2}", num1, num2,
Array.ConvertAll((num1 * num2)
.ToString()
.ToCharArray()
.Where(w => Convert.ToInt32(w) % 2 != 0).ToArray(), c => (int)Char.GetNumericValue(c))
.Sum());


Console.ReadKey();

}
}
}


Output
------------
The sum of odd digits of the product of 19 and 65 is 9

Comments or Responses

Login to post response