Input name of a customer and purchase amount. Find discount and net amount.
Discount rate is 10% of the purchase amount. Print all details.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name and purchase amount");
String na=sc.nextLine();
double amt=sc.nextDouble();
double dis=10/100.0*amt;
double net=amt-dis;
System.out.println("Name: "+na);
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2. Conditional Construct: Selection Statement
2.1. if
Input purchase amount. Find discount and net amount using simple if.
If amount is 5000 or above then discount rate is 10% of the amount (otherwise no need to calculate discount).
Print purchase amount, discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis=0.0;
if(amt>=5000)
dis=10/100.0*amt;
double net=amt-dis;
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.2. if else
Input purchase amount. Find discount and net amount.
If amount is 5000 or above then discount rate is 10% of the amount, otherwise 8%.
Print purchase amount, discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis;
if(amt>=5000)
dis=10/100.0*amt;
else
dis=8/100.0*amt;
double net=amt-dis;
System.out.println("Purchase Amount: "+amt);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.3. if else if else
Input purchase amount. Find discount and net amount. Discount rate is as follows:
If amount is 5000 or above then 10% of the amount
If it is above 3000 then 8%
Otherwise 5% Print discount and net amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount");
double amt=sc.nextDouble();
double dis;
if(amt>=5000)
dis=10/100.0*amt;
else if(amt>3000)
dis=8/100.0*amt;
else
dis=5/100.0*amt;
double net=amt-dis;
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.4. Nested if else if
Input purchase amount and category. Find discount and net amount; print details. The rate is given below:
Amount Gents Dress Ladies Dress
5000 or above 10% 12%
3001 to 4999 8% 10%
3000 and below 5% 8%
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter purchase amount and category [G/L]");
double amt=sc.nextDouble();
char category=sc.next().charAt(0);
double dis;
if(category==‘G’ || category==‘g’)
{
if(amt>=5000)
dis=10/100.0*amt;
else if(amt>3000)
dis=8/100.0*amt;
else
dis=5/100.0*amt;
}
else if(category==‘L’ || category==‘l’)
{
if(amt>=5000)
dis=12/100.0*amt;
else if(amt>3000)
dis=10/100.0*amt;
else
dis=8/100.0*amt;
}
else
{
System.out.println("No discount");
dis=0.0;
}
double net=amt-dis;
System.out.println("Purchase Amount: "+ amt);
System.out.println("Category: "+ category);
System.out.println("Discount: "+dis);
System.out.println("Net Amount: "+net);
}
}
2.5. switch case
Write a menu-driven program: 1) Calculate discount and net amount. Discount rate is 10% on the amount
2) Calculate tax and total. Tax is 12% on the amount.
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("1 Discount");
System.out.println("2 Tax");
System.out.println("Enter your choice");
int ch=sc.nextInt();
double amt,dis,net,tax,tot;
switch(ch)
{
case 1: System.out.println("Enter purchase amount");
amt=sc.nextDouble();
dis=10/100.0*amt;
net=amt-dis;
System.out.println("Discount: "+dis);
System.out.println("Net amount: "+net);
break;
case 2: System.out.println("Enter purchase amount");
amt=sc.nextDouble();
tax=12/100.0*amt;
tot=amt+tax;
System.out.println("Tax: "+tax);
System.out.println("Total: "+tot);
break;
default: System.out.println("Wrong input");
)
}
}
3. Iterative Construct (Loop):
3.1. for
Input name and purchase amount of n customers. If amount is 5000 or above then discount
rate is 10% of the amount, if it is above 3000 then 8% otherwise 5%. Print bill for each customer. Also find and print total net amount collected before closing the shop. Bill Format is given below:
Sl.No.NameAmountDiscountNet Amount
1xxxxxxxxxxxx
Total net amount collected: xxx
import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of customers");
int n=sc.nextInt();
double tot=0.0;
for(int i=1;i<=n;i++)
{
System.out.println("Enter name and purchase amount");