VB.net program to find the sum of odd digits of the product of two numbers.

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 1215
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

Imports System.Linq
Imports System.Numerics

Namespace ConsoleApplication2
Class Program

Private Shared Sub Main(args As String())

Dim num1 As BigInteger = 19
Dim num2 As BigInteger = 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(Function(w) Convert.ToInt32(w) Mod 2 <> 0).[Select](Function(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(Function(w) Convert.ToInt32(w) Mod 2 <> 0).ToArray(), Function(c) CInt(Math.Truncate([Char].GetNumericValue(c)))).Sum())


Console.ReadKey()

End Sub
End Class
End Namespace


Output
------

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

Comments or Responses

Login to post response