In: Computer Science
Lodging in Europe
Q1. Write a program to make suggestions on lodging in Europe.
Note that to compare whether a String variable country equals "France", you cannot use country == "France". The correcr way to compare is country.equals("France").
Q2.
Days of a Month
Given the year and month number, return the number of days in the month.
A year is a leap year if the year number is a multiple of 4 but not a multiple of 100. However, if a year number is a multiple of 400, the year is a leap year.
Since you have not mentioned the language of your preference, I am providing the code in Java.
1)
CODE
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double spend;
System.out.println("Enter the amount you want to spend on lodging per night: ");
spend = sc.nextDouble();
if (spend <= 30) {
System.out.println("Go to camping");
} else if (spend <= 45) {
System.out.println("Enter your age: ");
int age = sc.nextInt();
if (age <= 30) {
System.out.println("Take youth hostel");
} else {
System.out.println("Take adult hostel");
}
} else if (spend <= 100) {
System.out.println("Hotel");
} else if (spend <= 200) {
System.out.println("Grand hotel");
} else {
System.out.println("Enter the country: ");
String country = sc.nextLine();
if ("France".equalsIgnoreCase(country)) {
System.out.println("Go to palaces");
} else {
System.out.println("Take exclusive suites");
}
}
}
}
2)
CODE
import java.util.Scanner;
public class Main {
public static boolean isLeapYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is muliplt of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is muliplt of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the month: ");
int month = sc.nextInt();
System.out.println("Enter the year: ");
int year = sc.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.println("Number of days = 31");
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
System.out.println("Number of days = 30");
} else {
if (isLeapYear(year)) {
System.out.println("Number of days = 29");
} else {
System.out.println("Number of days = 28");
}
}
}
}