In: Computer Science
Write a program that asks the user to enter an item’s wholesale cost and the markup percentage. It should then display the item’s retail price, which also includes a sales tax. For example, if the item’s wholesale cost is $5.00 and it’s markup is 100 percent, than the item’s retail price will be the marked up cost ($10.00) plus the sales tax (assume 6%), which would be $10.60). If an item’s wholesale cost is $10.00 and it’s markup is 50 percent, then the retail cost would be $15.90.
The program should have a method named “calculateRetail” that receives the wholesale cost and markup percentage as arguments and return the retail price of the item.
The programming language is not mentioned. Assuming it to be solved using C++. For any other languages (like JAVA or C), the structure of the program would be same, except that the keywords will be different.
The task is to get the item's wholesale cost and markup percentage. Markup percentage is calculated with respect to the wholesale cost. Also, sales tax are there (assuming 6%) which is applicable on the wholesale plus markup cost.
The required program (in
C++) is given below:
#include<iostream>
//importing basic packages
#include<iomanip>
using namespace std;
double calculateRetail(double wholesale, double
markup)
//function to calculate the retail price
{
double r;
r=wholesale+wholesale*markup/100;
//considering the markup percentage
r=r+r*0.06;
//consideration of the sales tax on the whole calculated
value
return r;
//returning the value
}
int main()
//main method
{
double ws, mp, rp;
cout<<"Enter the wholesale cost of the item:
";
cin>>ws;
//user
input of the wholesale amount
if(ws<=0)
//if the input is invalid,
exit the program
{
cout<<"Please enter a valid
wholesale cost value by running it again."<<endl;
return 0;
}
cout<<"Enter the markup percentage: ";
cin>>mp;
//user
input of the markup percentage
if(mp<0) //if the
input is invalid, exit the program
{
cout<<"Please enter a valid
markup percentage by running it again."<<endl;
return 0;
}
rp=calculateRetail(ws, mp);
//calculating the retail
price by calling the function and passing arguments
cout<<fixed<<setprecision(2)<<endl;
//setting
the result upto 2 decimals
cout<<"The calculated Retail Price is:
$"<<rp;
//displaying the result
return 0;
}
Screenshot of the
code:
It is assumed that correct markup percentage cannot be
negative.
Output 1:
Output 2:
Output 3 (invalid input):
Output 4 (invalid input):
The same solution in JAVA is given below:
import
java.util.*;
//import basic package
public class
Main
//defining a class
{
public static double calculateRetail(double
wholesale, double markup) //function to calculate the
retail price
{
double r;
r=wholesale+wholesale*markup/100;
//considering the markup percentage
r=r+r*0.06;
//consideration of the sales tax on the whole calculated
value
return
r;
//returning the value
}
public static void main(String
[]args) //
defining main method
{
double ws, mp, rp;
Scanner sc=new
Scanner(System.in);
//taking a Scanner object for input purpose
System.out.printf("Enter
the wholesale cost of the item: ");
ws=sc.nextDouble();
//user input of the wholesale amount
if(ws<=0)
//if the input is invalid, exit the program
{
System.out.printf("Please enter a valid wholesale cost value by
running it again.");
System.exit(0);
}
System.out.printf("Enter
the markup percentage: ");
mp=sc.nextDouble();
//user input of the markup percentage
if(mp<0) //if the
input is invalid, exit the program
{
System.out.printf("Please enter a valid markup percentage by
running it again.");
System.exit(0);
}
rp=calculateRetail(ws,
mp); //calculating the retail price by
calling the function and passing arguments
System.out.printf("The
calculated Retail Price is:
$%.2f",rp); //displaying the result
upto 2 decimals
}
}
Screenshot of the JAVA code:
Output of the JAVA code: