Java program to calculate discount of a product. With the help of the following program, you can calculate the discount of a product instantly. The following code has been written in five different ways. By using standard values, using command line arguments, while loop, do while loop, user-defined method and creating a separate class.
How to calculate a discount on a product?
A: Discount is defined as the reduction in the price something you sell to the customers. Most of the discount rate is given in percentage rate.
Here is the formula to calculate the discount:
Discount = List price x Discount Rate
For example :
A pen costs 50$ and it is been sold at a discount of 12%, what is the discount price of the pen?
A: = 50 x 12/100
= 50 × 0.12
= $ 6
Discount price of the pen is : 44$. Hope you get it.
Okay fine! Let’s get into the programming for the above equation is different formats.
Discount Java Code
1. Java program to calculate Discount ( Using standard values )
By using standard values consider this program is universal code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Discount { public static void main(String args[]) { double dis,amount,markedprice,s; markedprice=1000; dis=25; // 25 mean 25% System.out.println("markedprice= "+markedprice); System.out.println("discount rate="+dis); s=100-dis; amount= (s*markedprice)/100; System.out.println("amount after discount="+amount); } } |
1 2 3 |
markedprice = 1000.0 discount rate=25.0 amount after discount=750.0 |
2. Taking inputs through scanner classs
Taking inputs through scanner class, with sample output.
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 |
java.util.Scanner; class Discount { public static void main(String args[]) { double dis,amount,markedprice,s; Scanner sc=new Scanner(System.in); System.out.println("enter markedprice "); markedprice=sc.nextDouble(); System.out.println("enter discount percentage "); dis=sc.nextDouble(); s=100-dis; amount= (s*markedprice)/100; System.out.println("amount after discount="+amount); } } |
output:
1 2 3 4 5 |
enter marked price 2000 enter discount percentage 30 amount after discount=1400.0 |
3. By Using Command-Line Arguments
There you go another method: By using command line arguments. If you have no idea about command line arguments you can check out here – command line arguments with examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Discount { public static void main(String args[]) { double dis,amount,markedprice,s; markedprice=Double.parseDouble(args[0]); dis=Double.parseDouble(args[1]); System.out.println("markedprice= "+markedprice); System.out.println("discount rate="+dis); s=100-dis; amount= (s*markedprice)/100; System.out.println("amount after discount="+amount); } } |
output:
1 2 3 |
markedprice= 10000.0 discount rate=50.0 amount after discount=5000.0 |
4.User Define Method
By using user-defined method. Here you go:
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 |
class Discount { public static void main(String args[]) { double dis,amount,markedprice,s; markedprice=Double.parseDouble(args[0]); dis=Double.parseDouble(args[1]); System.out.println("markedprice= "+markedprice); System.out.println("discount rate="+dis); s=100-dis; amount= calcuateDiscount(markedprice,s); System.out.println("amount after discount="+amount); } static double calcuateDiscount(double markedprice,double s) { double amount= (s*markedprice)/100; return amount; } } |
output:
1 2 3 |
markedprice= 10000.0 discount rate=15.0 amount after discount=8500.0 |
5. Inputs Through Scanner Class
Java code for getting discount of product through creating a separate class( ) and taking inputs through scanner class.
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 35 36 |
class calcuateDiscount { double amount; calcuateDiscount(double markedprice,double s) { amount= (s*markedprice)/100; } } class Discount { public static void main(String args[]) { double dis,amount,markedprice,s; markedprice=Double.parseDouble(args[0]); dis=Double.parseDouble(args[1]); System.out.println("markedprice= "+markedprice); System.out.println("discount rate="+dis); s=100-dis; calcuateDiscount c=new calcuateDiscount(markedprice,s); System.out.println("amount after discount="+c.amount); } } |
1 2 3 |
markedprice= 1000.0 discount rate=10.0 amount after discount=900.0 |
More programs :