L8: Selection Control
Q1. Write a program that reads a number from a user and prints “Hi Five” if the number is divisible by 5. It also prints “Hi Seven” if the number is divisible by 7.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int num=ip.nextInt();
if(num%5==0)
System.out.println("Hi Five");
if(num%7==0)
System.out.println("Hi Seven");
}
}
Q2. Write a program that reads the body temperature of a patient and prints that “The patient is suffering from fever” if the input temperature is greater than 99 degree Fahrenheit.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the temperature in Fahrenheit: ");
double temp=ip.nextDouble();
if(temp>99)
System.out.println("The patient is suffering from fever");
}
}
Q3. Write a program to read a number from a user and verify whether the input number is odd or even.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int num=ip.nextInt();
if(num%2==0)
System.out.println("The number is Even");
else
System.out.println("The number is Odd");
}
}
Q4. Write a program that reads a number and prints the absolute value of that number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int num=ip.nextInt();
if(num>0)
System.out.println("The absolute value is: "+num);
else
System.out.println("The absolute value is: "+(-num));
}
}
Q5. Write a program that reads two numbers as input and determines the maximum number.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int n1=ip.nextInt();
int n2=ip.nextInt();
int max;
if(n1>n2)
max=n1;
else
max=n2;
System.out.print("Maximum number is: "+max);
}
}
Q6. Write a program that determines if a given year is a leap year or not.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a year: ");
int year=ip.nextInt();
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
System.out.println(year+ " is a leap year");
else
System.out.println(year+ " is not a leap year");
}
}
Q7. Write a program that reads three given lengths from a user and verify whether these lengths can form a valid triangle.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter three lengths: ");
double a=ip.nextDouble();
double b=ip.nextDouble();
double c=ip.nextDouble();
if(a+b>c && c+a>b && b+c>a)
System.out.print("The given lengths can form a triangle");
else
System.out.print("The given lengths cannot form a triangle");
}
}
Q8. Write a program that computes the ticket price of a visitor by taking entry time and age. The ticket price is 20 if the entry time is before 12, otherwise it is 30. The ticket price is 10 for senior citizens and children if the entry time is before 12.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter visitors age and entry time: ");
int age=ip.nextInt();
double time=ip.nextDouble();
int price;
if(time<12){
if(age<12 || age>60)
price=10;
else
price=20;
}
else
price=30;
System.out.println("Ticket price is: "+price);
}
}
Q9. Write a program for a product discount checker according to the following rules: if the quantity purchased is 10 or more and the total cost is greater than 1000, apply a discount of 10%. On the other hand, when the total cost is less than 1000 for 10 quantities, the discount is 5%. Note that, if the purchased quantity is less than 10, no discount is applied. Your program should ask the user to input the price of an item and the quantity purchased. Based on these inputs, the program should display the total cost and the final cost after applying discounts.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the price and quantity: ");
double price=ip.nextDouble();
int quantity=ip.nextInt();
double total_cost=quantity*price;
double discount;
if(quantity>10){
if(total_cost>1000)
discount=0.1*total_cost;
else
discount=0.05*total_cost;
}
else
discount=0;
System.out.println("Total cost is: "+total_cost);
System.out.println("Final cost after discount is: "+(total_cost-discount));
}
}
Q10. Guidelines issued by RBI to collect ATM transaction fees from the next financial year are as follows. For a savings account: if the withdrawal amount is less than 2000 the transaction fee is 5% of the withdrawal amount, otherwise 7% of the withdrawal amount. For a current account: if the withdrawal amount is less than 2500 the transaction fee is 2% of the withdrawal amount, otherwise it is 3% of the withdrawal amount. Write a program that calculates the ATM transaction fee by taking withdrawal amount and type of account as input.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter type of your account(savings/current): ");
String account_type=ip.next();
System.out.print("Enter withdrawal amount: ");
int amount=ip.nextInt();double transaction_fee;
double discount;
if(account_type=="savings"){
if(amount<2000)
transaction_fee=0.05*amount;
else
transaction_fee=0.07*amount;
}
else{
if(amount<2500)
transaction_fee=0.02*amount;
else
transaction_fee=0.03*amount;
}
System.out.println("The ATM transaction fee is: "+transaction_fee);
}
}
Q11. Write a program that reads a number and identifies the given number as positive, negative, or zero.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number: ");
int n=ip.nextInt();
if (n>0)
System.out.println("The number is positive");
else
if (n<0)
System.out.println("The number is negative");
else
System.out.println("You have entered zero");
}
}
Q12. Write a program that takes a student's mark out of 100 and determines the corresponding grade.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the mark: ") ;
int mark=ip.nextInt();
char grade;
if(mark>90)
grade='A';
else if(mark>80)
grade='B';
else if(mark>70)
grade='C';
else if(mark>60)
grade='D';
else
grade='F';
System.out.print("The equivalent grade is: "+grade);
}
}
Q13. Write a program that takes a person's age as input and classifies them into categories like infant, child, teenager, adult, or senior citizen based on certain age ranges.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the age: ");
int age=ip.nextInt();
if(age<2)
System.out.println("Infant");
else if(age<13)
System.out.println("Child");
else if(age<19)
System.out.println("Teenager");
else if (age<61)
System.out.println("Adult");
else
System.out.print("Senior citizen");
}
}
Q14. Write a program that reads time from a user (in 24 hour format) and greets the user based on the time of the day.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter the current time: ") ;
double time=ip.nextDouble();
if(time>0 && time<24){
if(time<12)
System.out.println("Good Morning!");
else if(time<18)
System.out.println("Good Afternoon!");
else
System.out.println("Good Evening!");
}
else
System.out.println("Invalid Input");
}
}
Q15. Write a program that takes a day number (0-6) as input and uses switch case to display the corresponding day.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Enter a number between 0 to 6: ");
int num=ip.nextInt();
switch(num) {
case 0:
System.out.println("Sunday");
break;
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid input");
}
}
}
Q16. Write a program that simulates a traffic light system where the user inputs a color and based on the color the program displays the action e.g. stop, get ready, go using switch case.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Input a color (red, yellow or green): ");
String color=ip.next();
switch(color){
case "red":
System.out.println("Stop");
break;
case "yellow":
System.out.println("Get ready");
break;
case "green":
System.out.println("Go");
break;
default:
System.out.println("Invalid input");
}
}
}
Q17. Create a simple calculator program in which users can perform basic arithmetic operations (addition, subtraction, multiplication, division) using switch case by taking the operator and integer operands as input.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Input two integer operands: ");
int num1=ip.nextInt();
int num2=ip.nextInt();
System.out.print("Input the operator: ");
char op=ip.next().charAt(0);
switch(op){
case '+':
System.out.println("Result of addition is: "+ (num1+num2));
break;
case '-':
System.out.println("Result of subtrction is: "+(num1-num2));
break;
case '*':
System.out.println("Result of multiplication is: "+(num1*num2));
break;
case '/':
System.out.println("Result of division is: "+(num1/num2));
break;
default:
System.out.println("Invalid input");
}
}
}
Q18. Write a program that takes student’s mark as input and uses switch case to determine the grade (A, B, C, D, F) based on predefined score ranges.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Input the mark: ");
int mark=ip.nextInt();
char grade;
switch(mark/10){
case 10:
case 9:
grade='A';
break;
case 8:
grade='B';
break;
case 7:
grade='C';
break;
case 6:
grade='D';
break;
default:
grade='F';
}
System.out.println("Grade is: "+grade);
}
}
Q19. Write a program that takes a month number (1-12) as input and displays the corresponding season (Spring, Summer, Monsoon, Autumn, Winter) using switch case.
import java.util.Scanner;class Test {
public static void main(String[ ] args) {
Scanner ip=new Scanner(System.in);
System.out.print("Input the month number: ");
int number=ip.nextInt();
switch(number){
case 12:
case 1:
case 2:
System.out.println("Winter");
break;
case 3:
case 4:
System.out.println("Spring");
break;
case 5:
case 6:
System.out.println("Summer");
break;
case 7:
case 8:
System.out.println("Monsoon");
break;
case 9:
case 10:
case 11:
System.out.println("Autumn");
break;
default:
System.out.println("Invalid input");
}
}
}