In this tutorial, I’ll show you how to find the Multiply of all the items in a dictionary.
Firstly,declare and initialize a dictionary to have some key-value pairs. Then find the Multiply of all the values in the dictionary and print the Multiply of the values.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | class Program { static void Main(string[] args) { int mul = 1; // New Dictionary Dictionary<string, int> dic = new Dictionary<string, int>(); dic.Add("num1", 10); dic.Add("num2", 32); dic.Add("num3", 11); dic.Add("num4", 9); dic.Add("num5", 10); //Before Sorting Console.WriteLine("List of Dictionary"); Console.WriteLine("<::::::::::::::>"); foreach (var x in dic) { Console.WriteLine("Key: {0}, Value: {1}", x.Key, x.Value); } //add the values foreach (var item in dic) { mul *= item.Value; } Console.WriteLine("Total muliply of values in the dictionary:"+mul); Console.ReadLine(); } } |
Output: