Week 2 - Level 3 - 11 Practice Problems
Week 2 - Level 3 - 11 Practice Problems
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 java.util.Scanner;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
} else {
} else {
return "Leap Year";
} else {
} else {
scanner.close();
}
2. Rewrite program 1 to determine Leap Year with single if condition using logical and && and
or || operators
import java.util.Scanner;
scanner.close();
}
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 java.util.Scanner;
scanner.close();
// Calculate average percentage
grade = "A";
grade = "B";
grade = "C";
grade = "D";
grade = "E";
} else {
grade = "R";
// Display results
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 java.util.Scanner;
// Input number
scanner.close();
if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
// Output result
if (isPrime) {
} else {
}
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 java.util.Scanner;
scanner.close();
int sum = 0;
while (originalNumber != 0) {
if (sum == number) {
} else {
scanner.close();
int count = 0;
if (originalNumber == 0) {
count = 1;
} else {
while (originalNumber != 0) {
count++;
// Input weight in kg
// Input height in cm
// Calculate BMI
// Output BMI
} else {
scanner.close();
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 java.util.Scanner;
i.
j. public class HarshadNumber {
k. public static void main(String[] args) {
l. Scanner scanner = new Scanner(System.in);
m.
n. // Input number
o. System.out.print("Enter an integer: ");
p. int number = scanner.nextInt();
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. System.out.println(number + " is a Harshad Number.");
ee. } else {
ff. System.out.println(number + " is Not a Harshad
Number.");
gg. }
hh.
ii. scanner.close();
jj. }
kk. }
ll.
mm.
// Input number
int sum = 0;
if (number % i == 0) {
} else {
System.out.println(number + " is Not an Abundant
Number.");
scanner.close();
String op;
first = scanner.nextDouble();
System.out.print("Enter second number: ");
second = scanner.nextDouble();
op = scanner.next();
int number = 1;
switch (op) {
case "+":
break;
case "-":
break;
case "*":
break;
case "/":
if (second != 0) {
} else {
System.out.println("Error: Division by
zero");
break;
default:
System.out.println("Invalid Operator");
scanner.close();
}
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