Using function overloading calculate upto three levels of discount on a given cost.
Take all required inputs from main functions and invoke the function which calculates discounts using class object.
Java
import java.util.*;
public class Discount
{
double cost=0.0;
public void getDiscount(double a)
{
double amt=0.0;
amt=cost-((a/100)*cost);
System.out.println("Amount to be paid after single discount: "+amt);
}
public void getDiscount(double a,double b)
{
double amt=0.0;
amt=cost-((a/100)*cost)-((b/100)*cost);
System.out.println("Amount to be paid after double discount: "+amt);
}
public void getDiscount(double a,double b,double c)
{
double amt=0.0;
amt=cost-((a/100)*cost)-((b/100)*cost)-((c/100)*cost);
System.out.println("Amount to be paid after triple discount: "+amt);
}
public static void main(String args[])
{
double a=0,b=0,c=0;
Scanner sc=new Scanner(System.in);
Discount ob=new Discount();
System.out.print("Cost of item: ");
ob.cost=sc.nextInt();
System.out.print("First discount percentage: ");
a=sc.nextDouble();
System.out.print("Second discount percentage: ");
b=sc.nextDouble();
System.out.print("Third discount percentage: ");
c=sc.nextDouble();
ob.getDiscount(a);
ob.getDiscount(a,b);
ob.getDiscount(a,b,c);
}
}Java