Method Parameters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Method Parameters

Hello, It's again me! I have next problem: A store is running a promotion: if the total purchase price is equal to or exceeds 10000, the price will be discounted by 20%. The program you are given takes the total purchase price as input. Complete the given method to take the total purchase price as an argument, and calculate and return the discounted price if campaign's requirement is satisfied. The method should return the same price if discount is not given. Sample Input 13000 Sample Output 10400 Explanation 13000>10000, so the discount should be given: 13000-(0.2*13000) = 10400. Hint You need to use if else statements inside the method. Don't forget to mention the value type for the method parameter before its name. Notice that method returns a value, so you need to use Console.Write()/Console.WriteLine() to execute the output. using System; namespace SoloLearn { class Program { static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method } //complete the method declaration static int Discount() { //complete the method body } } } That's a clear code

29th Mar 2021, 2:57 PM
Sebastian Górski
Sebastian Górski - avatar
10 Answers
+ 8
Sebastian Górski It's simple. You already have a method and you just need to pass parameter in that method. As this method has return type int so you must return a integer value. As program said get discounted amount if amount is greater than 10000 otherwise return original amount. So your complete method should be like this static int Discount (int amount) { if (amount > 10000) { return (amount - 20 * amount / 100); } return amount; } And call this method inside main like Console.WriteLine(Discount(totalPrice));
29th Mar 2021, 6:30 PM
A͢J
A͢J - avatar
+ 8
If anyone needs a working code, my answer looks like this: static void Main(string[] args) { double totalPrice = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(Discount(totalPrice)); } static double Discount(double totalPrice) { if (totalPrice >= 10000){ double Discount = totalPrice - (totalPrice * 0.2); return Discount; } else return totalPrice; } For some reason, only the double type works correctly.
5th Apr 2021, 8:15 PM
popos
popos - avatar
+ 3
namespace SoloLearn { class Program { static void Main(string[] args) { double totalPrice = Convert.ToDouble(Console.ReadLine()); //call the method Console.WriteLine(Discount(totalPrice)); } //complete the method declaration static double Discount( double x) { //complete the method body if ( x >= 10000){ double dd = x-(0.2*x); return dd; } return x; } } }
23rd Jun 2021, 11:35 PM
Nargiza Ozek
+ 2
is it just me that thinks that this part of the course was bad written and they could have done a better work
24th Oct 2022, 7:33 PM
Elias Oliveira
Elias Oliveira - avatar
+ 1
Mehhh, I make 2 typo in my code... That's why it didn't work. You are right... I'm sorry for your lost time, and thank you so much for help..
29th Mar 2021, 6:41 PM
Sebastian Górski
Sebastian Górski - avatar
+ 1
Could you tell me static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Discount(totalPrice)); This is still a procedure? Or not?
29th Mar 2021, 9:40 PM
Sebastian Górski
Sebastian Górski - avatar
0
Sebastian Górski I think you already got your answer then why again asking same problem and why deleted previous one. I gave you right solution then what was mistake there?.
29th Mar 2021, 3:00 PM
A͢J
A͢J - avatar
0
That was my mistake bro :/ I still have a problem to make a program
29th Mar 2021, 6:09 PM
Sebastian Górski
Sebastian Górski - avatar
0
/*Method Parameters A store is running a promotion: if the total purchase price is equal to or exceeds 10000, the price will be discounted by 20%. The program you are given takes the total purchase price as input. Complete the given method to take the total purchase price as an argument, and calculate and return the discounted price if campaign's requirement is satisfied. The method should return the same price if discount is not given. Sample Input 13000 Sample Output 10400 Explanation 13000>10000, so the discount should be given: 13000-(0.2*13000) = 10400. Hint You need to use if else statements inside the method.*/ using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Double totalPrice = Convert.ToDouble(Console.ReadLine()); //call the method Console.WriteLine(Discount(totalPrice)); } //complete the method declaration static Double Discount(Double x) { //complete the method body if (x>=10000) { x = x - (0.2*x); } return x; } } }
15th Nov 2022, 8:38 PM
Chaitanya Bhoi
0
class Program { static void Main(string[] args) { int totalPrice = Convert.ToInt32(Console.ReadLine()); //call the method Console.WriteLine(Discount(totalPrice)); } //complete the method declaration static int Discount(int totalPrice) { //complete the method body if (totalPrice > 10000) { double Sale = totalPrice - (0.2 * totalPrice); return Convert.ToInt32(Sale); } else { return totalPrice; } } } } I tried the above one but still its failing, can someone help??
20th Nov 2022, 7:14 PM
Farhath Aamir
Farhath Aamir - avatar