Write a program that prompts the user to enter the price of an item and the discount offered as a percentage (on a scale of 0 to 100). The program should then calculate and display the new price.
Solution
The discount amount can be easily calculated. You must multiply the before-discount price of the product by the discount value and then divide it by 100. The division is necessary since the user enters a value for the discount on a scale of 0 to 100. Be careful—the result is not the final price but only the discount amount.
The final after-discount price can be calculated by subtracting the discount amount that you calculated beforehand from the initial before-discount price.
PHP
<?php
echo "Enter the price of a product: ";
$price_before_discount = trim(fgets(STDIN));
echo "Enter the discount offered (0 - 100): ";
$discount = trim(fgets(STDIN));
$discount_amount = $price_before_discount * $discount / 100;
$price_after_discount = $price_before_discount - $discount_amount;
echo "The price after discount is: ", $price_after_discount;
?>
Java
public static void main(String[] args) throws java.io.IOException {
java.io.BufferedReader cin = new java.io.
BufferedReader(new java.io.InputStreamReader(System.in));
int discount;
double discount_amount, price_after_discount, price_before_discount;
System.out.print("Enter the price of a product: ");
price_before_discount = Double.parseDouble(cin.readLine());
System.out.print("Enter the discount offered (0 - 100): ");
discount = Integer.parseInt(cin.readLine());
discount_amount = price_before_discount * discount / 100;
price_after_discount = price_before_discount - discount_amount;
System.out.println("The price after discount is: " + price_after_discount);
}
C++
#include <iostream>
using namespace std;
int main() {
int discount;
double discount_amount, price_after_discount, price_before_discount;
cout << "Enter the price of a product: ";
cin >> price_before_discount;
cout << "Enter the discount offered (0 - 100): ";
cin >> discount;
discount_amount = price_before_discount * discount / 100;
price_after_discount = price_before_discount - discount_amount;
cout << "The price after discount is: " << price_after_discount;
return 0;
}
C#
static void Main() {
int discount;
double discount_amount, price_after_discount, price_before_discount;
Console.Write("Enter the price of a product: ");
price_before_discount = Double.Parse(Console.ReadLine());
Console.Write("Enter the discount offered (0 - 100): ");
discount = Int32.Parse(Console.ReadLine());
discount_amount = price_before_discount * discount / 100;
price_after_discount = price_before_discount - discount_amount;
Console.Write("The price after discount is: " + price_after_discount);
Console.ReadKey();
}
Visual Basic
Sub Main()
Dim discount As Integer
Dim discount_amount, price_after_discount, price_before_discount As Double
Console.Write("Enter the price of a product: ")
price_before_discount = Console.ReadLine()
Console.Write("Enter the discount offered (0 - 100): ")
discount = Console.ReadLine()
discount_amount = price_before_discount * discount / 100
price_after_discount = price_before_discount - discount_amount
Console.Write("The price after discount is: " & price_after_discount)
Console.ReadKey()
End Sub
Python
price_before_discount = float(input("Enter the price of a product: "))
discount = int(input("Enter the discount offered (0 - 100): "))
discount_amount = price_before_discount * discount / 100
price_after_discount = price_before_discount - discount_amount
print("The price after discount is:", price_after_discount)