Conditional Stat Programs PDF
Conditional Stat Programs PDF
Conditional Stat Programs PDF
Write a program to input three angles of a triangle and check whether a triangle is possible or not. If possible then
check whether it is an acute-angled triangle, right-angled or an obtuse-angled triangle otherwise, display 'Triangle
not possible'.
Question 2
Write a program to input the cost price and the selling price of an article. If the selling price is more than the cost
price then calculate and display actual profit and profit per cent otherwise, calculate and display actual loss and loss
per cent. If the cost price and the selling price are equal, the program displays the message 'Neither profit nor loss'.
import java.util.Scanner;
public class Profit
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter cost price of the article: ");
double cp = in.nextDouble();
System.out.print("Enter selling price of the article: ");
double sp = in.nextDouble();
double pl = sp - cp;
double percent = Math.abs(pl) / cp * 100;
if (pl > 0)
{
System.out.println("Profit = " + pl);
System.out.println("Profit % = " + percent);
}
else if (pl < 0)
{
System.out.println("Loss = " + Math.abs(pl));
System.out.println("Loss % = " + percent);
}
else
{
System.out.println("Neither profit nor loss");
}
}
}
Question 3
Write a program to input three numbers and check whether they are equal or not. If they are unequal numbers
then display the greatest among them otherwise, display the message 'All the numbers are equal'.
Sample Input: 34, 87, 61
Sample Output: Greatest number: 87
Sample Input: 81, 81, 81
Sample Output: All the numbers are equal.
import java.util.Scanner;
public class Numbers
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
if (a == b && b == c)
{
System.out.println("All the numbers are equal");
}
else
{
int g = a;
if (b > g)
g = b;
if (c > g)
g = c;
System.out.println("Greatest number: " + g);
}
}
}
Question 4
Write a program to accept a number and check whether the number is divisible by 3 as well as 5. Otherwise,
decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.
import java.util.Scanner;
public class Divisor
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
if (num % 3 == 0 && num % 5 == 0)
System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}
Question 5
Write a program to input year and check whether it is:
(a) a Leap year (b) a Century Leap year (c) a Century year but not a Leap year
Sample Input: 2000
Sample Output: It is a Century Leap Year.
import java.util.Scanner;
public class LeapYear
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the year to check: ");
int yr = in.nextInt();
if (yr % 4 == 0 && yr % 100 != 0)
System.out.println("It is a Leap Year");
else if (yr % 400 == 0)
System.out.println("It is a Century Leap Year");
else if (yr % 100 == 0)
System.out.println("It is a Century Year but not a Leap Year");
else
System.out.println("It is neither a Century Year nor a Leap Year");
}
}
Question 6
Write a program to input two unequal positive numbers and check whether they are perfect square numbers or
not. If the user enters a negative number then the program displays the message 'Square root of a negative
number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.
import java.util.Scanner;
public class PerfectSquare
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
if (a < 0 || b < 0)
{
System.out.println("Square root of a negative number can't be determined");
}
else
{
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
double isAPerfectSq = sqrtA - Math.floor(sqrtA);
double isBPerfectSq = sqrtB - Math.floor(sqrtB);
if (isAPerfectSq == 0 && isBPerfectSq == 0)
{
System.out.println("They are perfect square numbers.");
}
else if (isAPerfectSq == 0)
{
System.out.println(a + " is a perfect square number.");
System.out.println(b + " is not a perfect square number.");
}
else if (isBPerfectSq == 0)
{
System.out.println(a + " is not a perfect square number.");
System.out.println(b + " is a perfect square number.");
}
else
{
System.out.println("Both are not perfect square numbers.");
}
}
}
}
Question 7
Without using if-else statement and ternary operators, accept three unequal numbers and display the second
smallest number.
[Hint: Use Math.max( ) and Math.min( )]
Sample Input: 34, 82, 61
Sample Output: 61
import java.util.Scanner;
public class 2ndSmallestNumber
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 3 unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();
int sum = a + b + c;
int big = Math.max(a, b);
big = Math.max(big, c);
int small = Math.min(a, b);
small = Math.min(small, c);
int result = sum - big - small;
System.out.println("Second Smallest Number = " + result);
}
}
Question 9
A Pre-Paid taxi charges from the passenger as per the tariff given below:
Distance Rate
Up to 5 km ₹ 100
For the next 10 km ₹ 10/km
For the next 10 km ₹ 8/km
More than 25 km ₹ 5/km
Write a program to input the distance covered and calculate the amount paid by the passenger. The program
displays the printed bill with the details given below:
Taxi No. :
Distance covered :
Amount :
import java.util.Scanner;
public class Taxi
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Taxi Number: ");
String taxiNo = in.nextLine();
System.out.print("Enter distance travelled: ");
int dist = in.nextInt();
int fare = 0;
if (dist <= 5)
fare = 100;
else if (dist <= 15)
fare = 100 + (dist - 5) * 10;
else if (dist <= 25)
fare = 100 + 100 + (dist - 15) * 8;
else
fare = 100 + 100 + 80 + (dist - 25) * 5;
System.out.println("Taxi No: " + taxiNo);
System.out.println("Distance covered: " + dist);
System.out.println("Amount: " + fare);
}
}
Question 10
A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost
as given below:
import java.util.Scanner;
public class Discount
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = in.nextDouble();
String gift;
double amt;
if (cost <= 2000.0)
{
amt = cost - (cost * 5 / 100);
gift = "Calculator";
}
else if (cost <= 5000.0)
{
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}
else if (cost <= 10000.0)
{
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}
else
{
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}
System.out.println("Amount to be paid: " + amt);
System.out.println("Gift: " + gift);
}
}
Question 11
Given below is a hypothetical table showing rate of income tax for an India citizen, who is below or up to 60 years.
import java.util.Scanner;
public class IncomeTax
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter age: ");
int age = in.nextInt();
System.out.print("Enter taxable income: ");
double ti = in.nextDouble();
double tax = 0.0;
if (age > 60)
{
System.out.print("Wrong Category");
}
else
{
if (ti <= 250000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10 / 100;
else if (ti <= 1000000)
tax = 34000 + ((ti - 500000) * 20 / 100);
else
tax = 94000 + ((ti - 1000000) * 30 / 100);
}
System.out.println("Name: " + name);
System.out.println("Tax Payable: " + tax);
}
}
Question 12
An employee wants to deposit certain sum of money under 'Term Deposit' scheme in Syndicate Bank. The bank has
provided the tariff of the scheme, which is given below:
import java.util.Scanner;
public class TermDeposit
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter sum of money: ");
double sum = in.nextDouble();
System.out.print("Enter number of days: ");
int days = in.nextInt();
double interest = 0.0;
if (days <= 180)
interest = sum * 5.5 / 100.0;
else if (days <= 364)
interest = sum * 7.5 / 100.0;
else if (days == 365)
interest = sum * 9.0 / 100.0;
else
interest = sum * 8.5 / 100.0;
double amt = sum + interest;
System.out.print("Maturity Amount = " + amt);
}
}
Question 13
Mr. Kumar is an LIC agent. He offers discount to his policy holders on the annual premium. However, he also gets
commission on the sum assured as per the given tariff.
import java.util.Scanner;
public class LIC
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Sum Assured: ");
double sum = in.nextDouble();
System.out.print("Enter First Premium: ");
double pre = in.nextDouble();
double disc = 0.0, comm = 0.0;
if(sum <= 100000)
{
disc = pre * 5.0 / 100.0;
comm = sum * 2.0 / 100.0;
}
else if(sum <= 200000)
{
disc = pre * 8.0 / 100.0;
comm = sum * 3.0 / 100.0;
}
else if(sum <= 500000)
{
disc = pre * 10.0 / 100.0;
comm = sum * 5.0 / 100.0;
}
else
{
disc = pre * 15.0 / 100.0;
comm = sum * 7.5 / 100.0;
}
System.out.println("Name of the policy holder: " + name);
System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + pre);
System.out.println("Discount on the first premium: " + disc);
System.out.println("Commission of the agent: " + comm);
}
}
Question 14
A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the
tariff given below:
import java.util.Scanner;
Question 15
Using a switch case statement, write a menu driven program to convert a given temperature from Fahrenheit to
Celsius and vice-versa. For an incorrect choice, an appropriate message should be displayed.
Hint: c = 5/9*(f-32) and f=1.8*c+32
import java.util.Scanner;
public class Temperature
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to convert from Fahrenheit to Celsius");
System.out.println("Type 2 to convert from Celsius to Fahrenheit");
int choice = in.nextInt();
double ft = 0.0, ct = 0.0;
switch (choice)
{
case 1:
System.out.print("Enter temperature in Fahrenheit: ");
ft = in.nextDouble();
ct = 5 / 9.0 * (ft - 32);
System.out.println("Temperature in Celsius: " + ct);
break;
case 2:
System.out.print("Enter temperature in Celsius: ");
ct = in.nextDouble();
ft = 1.8 * ct + 32;
System.out.println("Temperature in Fahrenheit: " + ft);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Question 16
The volume of solids, viz. cuboid, cylinder and cone can be calculated by the formula:
import java.util.Scanner;
public class Volume
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("1. Volume of cuboid");
System.out.println("2. Volume of cylinder");
System.out.println("3. Volume of cone");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;
case 2:
System.out.print("Enter radius of cylinder: ");
double rCylinder = in.nextDouble();
System.out.print("Enter height of cylinder: ");
double hCylinder = in.nextDouble();
double vCylinder = (22 / 7.0) * Math.pow(rCylinder, 2) * hCylinder;
System.out.println("Volume of cylinder = " + vCylinder);
break;
case 3:
System.out.print("Enter radius of cone: ");
double rCone = in.nextDouble();
System.out.print("Enter height of cone: ");
double hCone = in.nextDouble();
double vCone = (1 / 3.0) * (22 / 7.0) * Math.pow(rCone, 2) * hCone;
System.out.println("Volume of cone = " + vCone);
break;
default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}
Question 17
A Mega Shop has different floors which display varieties of dresses as mentioned
below:
Write a program to perform the above task as per the user's choice.
import java.util.Scanner;
Question 18
The equivalent resistance of series and parallel connections of two resistances are given by the formula:
(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)
Using a switch case statement, write a program to enter the value of r1 and r2. Calculate and display the equivalent
resistances accordingly.
import java.util.Scanner;
Write a program to input sum, rate, time and type of Interest ('S' for Simple Interest and 'C' for Compound
Interest). Calculate and display the sum and the interest earned.
import java.util.Scanner;
public class Interest
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the sum: ");
double p = in.nextDouble();
System.out.print("Enter rate: ");
double r = in.nextDouble();
System.out.print("Enter time: ");
int t = in.nextInt();
System.out.println("Enter type of Interest");
System.out.print("('S'- Simple Interest 'C'- Compound Interest): ");
char type = in.next().charAt(0);
boolean isTypeValid = true;
double interest = 0.0;
switch (type)
{
case 'S':
interest = p * r * t / 100;
break;
case 'C':
interest = p * (Math.pow((1 + (r / 100)), t) - 1);
break;
default:
isTypeValid = false;
System.out.println("Incorrect Interest type");
break;
}
if (isTypeValid)
{
double amt = p + interest;
System.out.println("Sum = " + p);
System.out.println("Interest = " + interest);
System.out.println("Sum + Interest = " + amt);
}
}
}
Question 20
'Kumar Electronics' has announced the following seasonal discounts on purchase of certain items.
Purchase Amount Discount on Laptop Discount on Desktop PC
Up to ₹ 25000 0.0% 5.0%
₹ 25,001 to ₹ 50,000 5% 7.5%
₹ 50,001 to ₹ 1,00,000 7.5% 10.0%
More than ₹ 1,00,000 10.0% 15.0%
Write a program to input name, amount of purchase and the type of purchase (`L' for Laptop and 'D' for Desktop)
by a customer. Compute and print the net amount to be paid by a customer along with his name.
(Net amount = Amount of purchase - discount)
import java.util.Scanner;
public class Sales
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Amount of Purchase: ");
double amt = in.nextDouble();
System.out.println("Enter Type of Purchase");
System.out.print("'L'- Laptop or 'D'- Desktop: ");
char type = in.next().charAt(0);
type = Character.toUpperCase(type);
double disc = 0.0;
if (amt <= 25000)
disc = type == 'L' ? 0.0 : 5.0;
else if (amt <= 50000)
disc = type == 'L' ? 5.0 : 7.0;
else if (amt <= 100000)
disc = type == 'L' ? 7.5 : 10.0;
else
disc = type == 'L' ? 10.0 : 15.0;
double netAmt = amt - (disc * amt / 100);
System.out.println("Name: " + name);
System.out.println("Net Amount: " + netAmt);
}
}