An employee needs a program to enter the before-tax price of a product and calculate its final price. Assume a value added tax (VAT) of 19%.
Solution
The sales tax can be easily calculated. You must multiply the before-tax price of the product by the sales tax rate. Be careful—the sales tax is not the final price, but only the tax amount.
The after-tax price can be calculated by adding the initial before-tax price and the sales tax that you calculated beforehand.
In this program you can use a constant named VAT
for the sales tax rate.
PHP
<?php
define("VAT", 0.19);
echo "Enter the before-tax price of a product: ";
$price_before_tax = trim(fgets(STDIN));
$sales_tax = $price_before_tax * VAT;
$price_after_tax = $price_before_tax + $sales_tax;
echo "The after-tax price is: ", $price_after_tax;
?>
Java
static final double VAT = 0.19;
public static void main(String[] args) throws java.io.IOException {
java.io.BufferedReader cin = new java.io.
BufferedReader(new java.io.InputStreamReader(System.in));
double price_after_tax, price_before_tax, sales_tax;
System.out.print("Enter the before-tax price of a product: ");
price_before_tax = Double.parseDouble(cin.readLine());
sales_tax = price_before_tax * VAT;
price_after_tax = price_before_tax + sales_tax;
System.out.println("The after-tax price is: " + price_after_tax);
}
Notice: Please note that the constant
VAT
is declared outside of the methodmain
.
C++
#include <iostream>
using namespace std;
const double VAT = 0.19;
int main() {
double price_after_tax, price_before_tax, sales_tax;
cout << "Enter the before-tax price of a product: ";
cin >> price_before_tax;
sales_tax = price_before_tax * VAT;
price_after_tax = price_before_tax + sales_tax;
cout << "The after-tax price is: " << price_after_tax;
return 0;
}
Notice: Please note that the constant
VAT
is declared outside of the functionmain
.
C#
const double VAT = 0.19;
static void Main() {
double price_after_tax, price_before_tax, sales_tax;
Console.Write("Enter the before-tax price of a product: ");
price_before_tax = Double.Parse(Console.ReadLine());
sales_tax = price_before_tax * VAT;
price_after_tax = price_before_tax + sales_tax;
Console.Write("The after-tax price is: " + price_after_tax);
Console.ReadKey();
}
Visual Basic
Const VAT = 0.19
Sub Main()
Dim price_after_tax, price_before_tax, sales_tax As Double
Console.Write("Enter the before-tax price of a product: ")
price_before_tax = Console.ReadLine()
sales_tax = price_before_tax * VAT
price_after_tax = price_before_tax + sales_tax
Console.Write("The after-tax price is: " & price_after_tax)
Console.ReadKey()
End Sub
Notice: Please note that the constant
VAT
is declared outside of the methodMain
.
Python
VAT = 0.19
price_before_tax = float(input("Enter the before-tax price of a product: "))
sales_tax = price_before_tax * VAT
price_after_tax = price_before_tax + sales_tax
print("The after-tax price is:", price_after_tax)