Conditional Construct

Section B

2006

Question 5
A cloth showroom has announced the following festival discounts on the purchase of items, based on the total cost of the items purchased:

Total costDiscount
5000 or above5%
Rs. 2001 to Rs. 500025%
Rs.5001 to Rs.1000035%
Above Rs. 1000050%

Write a program to input the name of the customer and cost, to compute and display the bill as follows.

NameCostNet amount
xxxxxxxxxxxxxxxxxxxxx[15]

import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter name and total cost : ");
String name= sc.nextLine();
double cost=sc.nextDouble();
double dis,net;
if(cost<2000)
dis=cost*5/100.0;
else if(cost<=5000)
dis=cost*25/100.0;
else if(cost<=10000)
dis=cost*35/100.0;
else
dis=cost*50/100.0;
net=cost-dis;
System.out.println("Name\tTotal cost\tDiscount\tNet amount ");
System.out.println(name+”\t”+cost+”\t”+dis+”\t”+net);
}
}

Variable Description

VariableTypeDescription
costdoublePurchase amt
disdoubleDiscount
netdoubleNet amount

2009

Question 4
An electronics shop has announced the following seasonal discounts on the purchase of certain items.

Purchase amountDiscount on LaptopDiscount on Desktop PC
0 – 250000%5%
25001 – 570005%7.5%
57001 – 1000007.5%10%
More than 10000010%15%

Write a program based on the above criteria, to input name, address, amount of purchase and type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the discount and net amount to be paid by a customer along with his name and address.
(Hint: discount = (discount rate/100)*amount of purchase
Net amount = amount of purchase - discount [15]

import java.util.*;
public class Discount
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter name, address, amount and type [D\L]: ");
String name=sc.nextLine();
String address=sc.nextLine();
double amt=sc.nextDouble();
char type=sc.next().charAt(0);
double dis,net;
if(type=='L'||type== ‘l’)
{
if(amt<=25000)
dis=0.0;
else if(amt<=57000)
dis=amt*5.0/100.0;
else if(amt<=100000)
dis=amt*7.5/100.0;
else
dis=amt*10.0/100.0;
}
else if(type=='D'||type== ‘d’)
{
if(amt<=25000)
dis=amt*5.0/100.0;
else if(amt<=57000)
dis=amt*7.5/100.0;
else if(amt<=100000)
dis=amt*10/100.0;
else
dis=amt*15.0/100.0;
}
else
{
System.out.println("Wrong input!"); dis=0.0;
}
net=amt-dis;
System.out.println("Name : "+name);
System.out.println("Address : "+address);
System.out.println("Purchase amount : "+amt);
System.out.println("Discount rate : "+rate);
System.out.println("Discount : "+dis);
System.out.println("Net amount : "+net);
}
}

2012

Question 5
Given below is a hypothetical table showing rates of income tax for male citizen below the age of 65 years:

Taxable Income (TI) in Rs.Income Tax in Rs.
Does not exceed Rs. 1,60,000Nil
Is greater than Rs. 1,60,000 & less than or equal to Rs 5,00,000(TI – 1,60,000) x 10%
Is greater than Rs. 5,00,000 & less than or equal to Rs 8,00,000[(TI – 5,00,000) x 20%] + 34,000
Is greater than Rs. 8,00,000[(TI – 8,00,000) x 30%] + 94,000

Write a program to input the age, gender (male or female) and Taxable Income of a person. If the age is more than 65 years or the gender is female, display “wrong category”. If the age is less than or equal to 65 years and the gender is male, compute and display the Income Tax payable as per the table given above. [15]

import java.util.*;
class IncomeTax
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter age,taxable income and gender[F/M]");
int age=sc.nextInteger();
double TI=sc.nextDouble();
char gender=sc.next().charAt(0);
double tax;
if(age<=65&&(gender=='M'||gender=='m'))
{
if(TI<=160000)
tax=0.0;
else if(TI<=500000) tax=(TI-160000)*10/100.0;
else if(TI<=800000)
tax=((TI-500000)*20/100.0)+34000;
else
tax=((TI-800000)*30/100.0)+94000;
System.out.println("Tax = "+tax);
}
else
System.out.println("Wrong category");
}
}

2014

Question 5
A special two-digit number is such that when the sum of its digits is added to the product of the digits, the result is equal to the original two-digit number.
Example: Consider the number 59. Sum of its digits = 5+9 = 14 Product of its digits = 5*9 = 45
Sum of the Sum of its digits and Product of its digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2 digit number” otherwise output the message “Not a special 2 digit number”. [15]

import java.util.*;
public class Numbers
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a two digit number");
int n= sc.nextInteger();
if(n>=10&&n<=99)
{
int d1,d2,sum,s,p;
d1=n/10;
d2=n%10;
s=d1+d2;
p=d1*d2;
sum=s+p;
if(sum==n)
System.out.println("Special 2 digit number”");
else
System.out.println("Not a special 2 digit number ");
}
else
System.out.println("Not a special 2 digit number ");
}
}