+ 1

Kaleidoscope Problem

I was solving the kaleidoscope problem in code coach.Why is it showing an error in test case 3 and 5 ? // program is solved by me... //here no external help is taken //because it was a easy problem #include <iostream> using namespace std; int main() { int n; //number of kaleido..... cin>>n; if(n==1){ float cost; cost=5+((5*7)/100.00); cout<<cost;//output-- 5.35 when cost is evaluated } else{ float cost; //calculate cost= n*5; //calculate discount float discount=cost/10.00;//since 10percent is equal to divide 10.... //now calculate cost cost= cost-discount; //tax of 7 percent //i am now not making another variable just directly calculating cost by adding tax 7 percent.... cost=cost + ((cost*7)/100.00); //printing cost price... cout<<cost; } return 0; } https://sololearn.com/compiler-playground/c1m9r4KiAMFJ/?ref=app

20th Apr 2024, 7:47 PM
Pralhad Saw
Pralhad Saw - avatar
2 Réponses
+ 1
Give you two clues. First clue Pralhad, ALL of your kaleidoscopes cost the same amount, 5.00! Second clue, CHECK the lesson in C++ course there is a lesson name Float vs Double. Complete the lesson again, read very carefully.
20th Apr 2024, 8:20 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
0
Hi Based on your description and the code you have provided, it appears that you are encountering errors in test cases 3 and 5 of your program. The problem could be due to precision issues with using float. You are using float for monetary calculations, which might not provide sufficient precision, especially when you start dealing with discounts and taxes. It is generally better to use double for more precise calculations when dealing with money. Your code treats the case where n == 1 differently by not applying any discount, which is correct, but you might be overlooking something in the logic or formatting when the calculations for discounts and taxes are applied for more than one kaleidoscope. Your outputs might not be consistently formatted to two decimal places. When it comes to financial calculations, it is common for the requirement to specify that the output should be formatted to two decimals.
20th Apr 2024, 8:22 PM
piano T