+ 2

Please can anyone explain to me why this code doesn't run

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int discount = Convert.ToInt32(Console.ReadLine()); Dictionary<string, int> coffee = new Dictionary<string, int>(); coffee.Add("Americano", 50); coffee.Add("Latte", 70); coffee.Add("Flat White", 60); coffee.Add("Espresso", 60); coffee.Add("Cappuccino", 80); coffee.Add("Mocha", 90); //your code goes here foreach (string item in coffee.Keys.ToArray()) { double disPrice = coffee[item] - coffee[item]*(discount/100); Console.WriteLine(item + ": " + disPrice); } } } } Thanks in advance

8th Oct 2022, 6:27 PM
Mr Ajiero
Mr Ajiero - avatar
2 Respostas
+ 2
Thanks a lot Jayakrishna🇮🇳 . I get it now. (Discount/100) = 0 for values < 100 because the resultant will be a double value less than 1. So it will be rounded down. Makes sense now
9th Oct 2022, 4:42 AM
Mr Ajiero
Mr Ajiero - avatar
+ 1
Code is running fine.. What is trouble you facing? pls save code share link for code problems. edit: Mr Ajiero discount is an int value. so int/int results int. (discount/100) result 0 for discount values less than 100. ( -99 to 99). Use atleast a double value so take discount as double type or use 100.0
8th Oct 2022, 8:14 PM
Jayakrishna 🇮🇳