Java Programs: Leap Year, Divisibility, and More
Checking for Leap Years in Java
This program allows the user to enter a year and checks whether it is a leap year:
import java.util.Scanner;
public class Leap {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = input.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 ");
}
}
}
Checking Divisibility by 5 and 6
This program prompts the user to enter an integer and checks whether the number is divisible by both 5 and 6, or neither, or just one of them.
import java.util.Scanner;
public class Exercise_03_26 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("Is " + number + " divisible by 5 and 6? " + ((number % 5 == 0) && (number % 6 == 0)));
System.out.println("Is " + number + " divisible by 5 or 6? " + ((number % 5 == 0) || (number % 6 == 0)));
System.out.println("Is " + number + " divisible by 5 or 6, but not both? " + ((number % 5 == 0) ^ (number % 6 == 0)));
}
}
Alternative version:
import java.util.Scanner;
public class Divisible5_6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int num = input.nextInt();
if (num % 5 == 0 && num % 6 == 0) {
System.out.println(num + " is divisible by 5 and 6");
} else if (num % 5 == 0) {
System.out.println(num + " is divisible by 5 only");
} else if (num % 6 == 0) {
System.out.println(num + " is divisible by 6 only");
} else {
System.out.println(num + " is neither divisible by 5 nor by 6");
}
}
}
Sorting Three Integers
This program sorts three integers entered by the user.
import java.util.Scanner;
public class Exercise_03_08 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int number3 = input.nextInt();
int temp;
if (number2 < number1) {
temp = number1;
number1 = number2;
number2 = temp;
}
if (number3 < number1) {
temp = number1;
number1 = number3;
number3 = temp;
}
if (number3 < number2) {
temp = number2;
number2 = number3;
number3 = temp;
}
System.out.println(number1 + " " + number2 + " " + number3);
}
}
Simple If Demo
This program prompts the user for an integer and displays “HiFive” if it’s a multiple of 5 and “HiEven” if it’s divisible by 2.
import java.util.Scanner;
public class SimpleIfDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = input.nextInt();
if (number % 5 == 0) {
System.out.println("HiFive");
}
if (number % 2 == 0) {
System.out.println("HiEven");
}
}
}
Calculating Gratuity and Total
This program reads the subtotal and gratuity rate, then computes the gratuity and total.
import java.util.Scanner;
public class Exercise_02_05 {
public static void main(String[] arg) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the subtotal and a gratuity rate: ");
double subtotal = input.nextDouble();
double gratuityRate = input.nextDouble();
double gratuity = subtotal * (gratuityRate / 100);
double total = subtotal + gratuity;
System.out.println("The gratuity is $" + gratuity + " and total is $" + total);
}
}