Level 3 Practice Programs
1. Write a LeapYear program that takes a year as input and outputs the Year is a Leap Year or
not a Leap Year.
Hint =>
a. The LeapYear program only works for year >= 1582, corresponding to a year in the
Gregorian calendar. So ensure to check for the same.
b. Further, the Leap Year is a Year divisible by 4 and not 100 unless it is divisible by 400.
E.g. 1800 is not a Leap Year and 2000 is a Leap Year.
c. Write code having multiple if else statements based on conditions provided above and
a second part having only one if statement and multiple logical
import [Link];
public class LeapYearChecker {
public static String isLeapYearIfElse(int year) {
if (year < 1582) {
return "Year should be 1582 or later";
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return "Leap Year";
} else {
return "Not a Leap Year";
} else {
return "Leap Year";
} else {
return "Not a Leap Year";
public static String isLeapYearLogical(int year) {
if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 ||
year % 400 == 0))) {
return "Leap Year";
} else if (year < 1582) {
return "Year should be 1582 or later";
} else {
return "Not a Leap Year";
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
[Link]();
[Link]("Using multiple if-else: " +
isLeapYearIfElse(year));
[Link]("Using single if with logical conditions:
" + isLeapYearLogical(year));
}
2. Rewrite program 1 to determine Leap Year with single if condition using logical and && and
or || operators
import [Link];
public class LeapYearChecker2 {
public static String isLeapYear(int year) {
if (year >= 1582 && (year % 4 == 0 && (year % 100 != 0 ||
year % 400 == 0))) {
return "Leap Year";
return year < 1582 ? "Year should be 1582 or later" : "Not a
Leap Year";
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a year: ");
int year = [Link]();
[Link]();
[Link]("Result: " + isLeapYear(year));
}
3. Write a program to input marks and 3 subjects physics, chemistry and maths. Compute the
percentage and then calculate the grade as per the following guidelines
Hint =>
a. Ensure the Output clearly shows the Average Mark as well as the Grade and Remarks
import [Link];
public class GradeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input marks for three subjects
[Link]("Enter marks for Physics: ");
int physics = [Link]();
[Link]("Enter marks for Chemistry: ");
int chemistry = [Link]();
[Link]("Enter marks for Maths: ");
int maths = [Link]();
[Link]();
// Calculate average percentage
double average = (physics + chemistry + maths) / 3.0;
// Determine grade and remarks
String grade, remarks;
if (average >= 80) {
grade = "A";
remarks = "Level 4, above agency-normalized standards";
} else if (average >= 70) {
grade = "B";
remarks = "Level 3, at agency-normalized standards";
} else if (average >= 60) {
grade = "C";
remarks = "Level 2, below, but approaching
agency-normalized standards";
} else if (average >= 50) {
grade = "D";
remarks = "Level 1, well below agency-normalized
standards";
} else if (average >= 40) {
grade = "E";
remarks = "Level 1-, too below agency-normalized
standards";
} else {
grade = "R";
remarks = "Remedial standards";
// Display results
[Link]("\nAverage Marks: " + average);
[Link]("Grade: " + grade);
[Link]("Remarks: " + remarks);
4. Write a Program to check if the given number is a prime number or not
Hint =>
a. A number that can be divided exactly only by itself and 1 are Prime Numbers,
b. Prime Numbers checks are done for number greater than 1
c. Loop through all the numbers from 2 to the user input number and check if the reminder
is zero. If the reminder is zero break out from the loop as the number is divisible by some
other number and is not a prime number.
d. Use isPrime boolean variable to store the result
import [Link];
public class PrimeNumberChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input number
[Link]("Enter a number: ");
int number = [Link]();
[Link]();
boolean isPrime = true;
// Prime number check for numbers greater than 1
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
// Output result
if (isPrime) {
[Link](number + " is a Prime Number.");
} else {
[Link](number + " is Not a Prime Number.");
}
5. Create a program to check if a number is armstrong or not. Use the hints to show the steps
clearly in the code
Hint =>
a. Armstrong Number is a number whose Sum of cubes of each digit results in the original
number as in for e.g. 153 = 1^3 + 5^3 + 3^3
b. Get an integer input and store it in the number variable and define sum variable, initialize
it to zero and originalNumber variable and assign it to input number variable
c. Use the while loop till the originalNumber is not equal to zero
d. In the while loop find the reminder number by using the modulus operator as in
number % 10. Find the cube of the number and add it to the sum variable
e. Again in while loop find the quotient of the number and assign it to the original number
using number / 10 expression. This romoves the last digit of the original number.
f. Finally check if the number and the sum are the same, if same its an Armstrong number
else not. So display accordingly
import [Link];
public class ArmstrongNumberChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Get input number
[Link]("Enter a number: ");
int number = [Link]();
[Link]();
int sum = 0;
int originalNumber = number;
// Loop to extract digits and calculate sum of cubes
while (originalNumber != 0) {
int digit = originalNumber % 10; // Get the last digit
sum += digit * digit * digit; // Cube the digit and add
to sum
originalNumber /= 10; // Remove last digit
// Check if the number is an Armstrong number
if (sum == number) {
[Link](number + " is an Armstrong Number.");
} else {
[Link](number + " is Not an Armstrong
Number.");
6. Create a program to count the number of digits in an integer.
Hint =>
a. Get an integer input for the number variable.
b. Create an integer variable count with value 0.
c. Use a loop to iterate until number is not equal to 0.
d. Remove the last digit from number in each iteration
e. Increase count by 1 in each iteration.
f. Finally display the count to show the number of digits
import [Link];
public class DigitCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Get input number
[Link]("Enter an integer: ");
int number = [Link]();
[Link]();
int count = 0;
int originalNumber = [Link](number); // Handle negative
numbers
// If the number is 0, it has 1 digit
if (originalNumber == 0) {
count = 1;
} else {
// Loop to count digits
while (originalNumber != 0) {
originalNumber /= 10; // Remove last digit
count++;
// Display the result
[Link]("Number of digits: " + count);
7. Create a program to find the BMI of a person
Hint =>
a. Take user input in double for the weight (in kg) of the person and height (in cm) for the
person and store it in the corresponding variable.
b. Use the formula BMI = weight / (height * height). Note unit is kg/m^2. For this convert cm
to meter
c. Use the table to determine the weight status of the person
import [Link];
public class BMI {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input weight in kg
[Link]("Enter weight in kg: ");
double weight = [Link]();
// Input height in cm
[Link]("Enter height in cm: ");
double heightCm = [Link]();
// Convert height to meters
double heightM = heightCm / 100;
// Calculate BMI
double bmi = weight / (heightM * heightM);
// Output BMI
[Link]("Your BMI is: %.2f%n", bmi);
// Determine weight status
if (bmi < 18.5) {
[Link]("Weight Status: Underweight");
} else if (bmi < 24.9) {
[Link]("Weight Status: Normal weight");
} else if (bmi < 29.9) {
[Link]("Weight Status: Overweight");
} else {
[Link]("Weight Status: Obesity");
[Link]();
8. Create a program to check if a number taken from the user is a Harshad Number.
Hint =>
a. A Harshad number is an integer which is divisible by the sum of its digits.
For example, 21 which is perfectly divided by 3 (sum of digits: 2 + 1).
b. Get an integer input for the number variable.
c. Create an integer variable sum with initial value 0.
d. Create a while loop to access each digit of the number.
e. Inside the loop, add each digit of the number to sum.
f. Check if the number is perfectly divisible by the sum.
g. If the number is divisible by the sum, print Harshad Number. Otherwise, print Not a
Harshad Number.
h. import [Link];
i.
j. public class HarshadNumber {
k. public static void main(String[] args) {
l. Scanner scanner = new Scanner([Link]);
m.
n. // Input number
o. [Link]("Enter an integer: ");
p. int number = [Link]();
q.
r. // Initialize sum variable
s. int sum = 0;
t. int temp = number;
u.
v. // Calculate sum of digits
w. while (temp > 0) {
x. sum += temp % 10; // Add last digit to sum
y. temp /= 10; // Remove last digit
z. }
aa.
bb. // Check if the number is divisible by the sum of its digits
cc. if (number % sum == 0) {
dd. [Link](number + " is a Harshad Number.");
ee. } else {
ff. [Link](number + " is Not a Harshad
Number.");
gg. }
hh.
ii. [Link]();
jj. }
kk. }
ll.
mm.
9. Create a program to check if a number is an Abundant Number.
Hint =>
a. An abundant number is an integer in which the sum of all the divisors of the number is
greater than the number itself. For example,
Divisor of 12: 1, 2, 3, 4, 6
Sum of divisor: 1 + 2 + 3 + 4 + 6 = 16 > 12
b. Get an integer input for the number variable.
c. Create an integer variable sum with initial value 0.
d. Run a for loop from i = 1 to i < number.
e. Inside the loop, check if number is divisible by i.
f. If true, add i to sum.
g. Outside the loop Check if sum is greater than number.
h. If the sum is greater than the number, print Abundant Number. Otherwise, print Not an
Abundant Number.
import [Link];
public class AbundantNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input number
[Link]("Enter an integer: ");
int number = [Link]();
// Initialize sum variable
int sum = 0;
// Calculate sum of divisors
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum += i; // Add divisor to sum
// Check if the sum of divisors is greater than the number
if (sum > number) {
[Link](number + " is an Abundant Number.");
} else {
[Link](number + " is Not an Abundant
Number.");
[Link]();
10.Write a program to create a calculator using switch...case.
Hint =>
a. Create two double variables named first and second and a String variable named op.
b. Get input values for all variables.
c. The input for the operator can only be one of the four values: "+", "-", "*" or "/".
d. Run a for loop from i = 1 to i < number.
e. Based on the input value of the op, perform specific operations using the switch...case
statement and print the result.
f. If op is +, perform addition between first and second; if it is -, perform subtraction and so
on.
g. If op is neither of those 4 values, print Invalid Operator.
import [Link];
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
double first, second;
String op;
// Get input values
[Link]("Enter first number: ");
first = [Link]();
[Link]("Enter second number: ");
second = [Link]();
[Link]("Enter operator (+, -, *, /): ");
op = [Link]();
// Assuming a number for the loop, for example 1
int number = 1;
for (int i = 1; i < number; i++) {
switch (op) {
case "+":
[Link]("Result: " + (first +
second));
break;
case "-":
[Link]("Result: " + (first -
second));
break;
case "*":
[Link]("Result: " + (first *
second));
break;
case "/":
if (second != 0) {
[Link]("Result: " + (first /
second));
} else {
[Link]("Error: Division by
zero");
break;
default:
[Link]("Invalid Operator");
[Link]();
}
11.Write a program DayOfWeek that takes a date as input and prints the day of the week
that the date falls on. Your program should take three command-line arguments: m
(month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For
output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following
formulas, for the Gregorian calendar (where / denotes integer division):
y0 = y − (14 − m) / 12
x = y0 + y0/4 − y0/100 + y0/400
m0 = m + 12 × ((14 − m) / 12) − 2
d0 = (d + x + 31m0 / 12) mod 7