Java Program to Calculate Discounted Price

In the previous article, we have seen Java Program to Calculate Percentage of Secured Mark

In this article we are going to see how to find discounted price using Java programming language.

Java Program to Calculate Discounted Price

To calculate the final price after discount, we will have to multiply the actual price with the discount and subtract it from the original price.

For example:

If the actual price is 500 and discount is 20%, final price will be: 
500 – (500 * 20/100) = 500 – 100 = 400.
 
20% means, you will get discount of 20 for a product priced at 100. 
And, for a product priced at 300, you will get discount 300*(20/100).

Let’s see the actual program to understand it more clearly.

Method-1: Java Program to Calculate Discounted Price By Using User Input Value

Approach:

  1. Create Scanner class object.
  2. Take user input for original and discounted price.
  3. Calculate discounted price by subtracting (originalPrice x discountPercentage/100) from the original price.
  4. Print the discounted price.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter original price
        System.out.print("Enter original price: ");
        double originalPrice = sc.nextDouble();
        // prompt user to enter discount percentage
        System.out.print("Enter discount percentage: ");
        double discountPercentage = sc.nextDouble();
        // calculate discounted price
        double discountedPrice = originalPrice - (originalPrice * discountPercentage / 100);
        // display discounted price
        System.out.println("Discounted price is " + discountedPrice);
    }

}
Output:

Enter original price: 500
Enter discount percentage: 10
Discounted price is 450.0

Method-2: Java Program to Calculate Discounted Price By Using User Defined Method

Approach:

  1. Create Scanner class object.
  2. Take user input for original and discounted price.
  3. Call user defined method discountPrice()
  4. Inside method calculate discounted price by subtracting (originalPrice x discountPercentage/100) from the original price.
  5. Print the discounted price.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter original price
        System.out.print("Enter original price: ");
        double originalPrice = sc.nextDouble();
        // prompt user to enter discount percentage
        System.out.print("Enter discount percentage: ");
        double discountPercentage = sc.nextDouble();
        //call discountPrice() method
        discountPrice(originalPrice,discountPercentage);
    }
    
    //discountPrice() method to find discounted price
    public static void discountPrice(double originalPrice, double discountPercentage) 
    {
        // calculate discounted price
        double discountedPrice = originalPrice - (originalPrice * discountPercentage / 100);
        // display discounted price
        System.out.println("Discounted price is " + discountedPrice);
    }

}
Output:

Enter original price: 1000
Enter discount percentage: 20
Discounted price is 800.0

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: