0% found this document useful (0 votes)
22 views18 pages

Week 2 - Level 3 - 11 Practice Problems

The document contains a series of programming exercises focused on Java, including tasks such as checking for leap years, calculating grades, determining prime and Armstrong numbers, counting digits, calculating BMI, and creating a calculator using switch-case. Each exercise includes hints and sample code to guide the implementation. The exercises are designed to enhance programming skills through practical applications of logic and control structures.

Uploaded by

thorodinsan82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views18 pages

Week 2 - Level 3 - 11 Practice Problems

The document contains a series of programming exercises focused on Java, including tasks such as checking for leap years, calculating grades, determining prime and Armstrong numbers, counting digits, calculating BMI, and creating a calculator using switch-case. Each exercise includes hints and sample code to guide the implementation. The exercises are designed to enhance programming skills through practical applications of logic and control structures.

Uploaded by

thorodinsan82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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 java.util.Scanner;

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(System.in);

System.out.print("Enter a year: ");

int year = scanner.nextInt();

scanner.close();

System.out.println("Using multiple if-else: " +


isLeapYearIfElse(year));

System.out.println("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 java.util.Scanner;

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(System.in);

System.out.print("Enter a year: ");

int year = scanner.nextInt();

scanner.close();

System.out.println("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 java.util.Scanner;

public class GradeCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input marks for three subjects

System.out.print("Enter marks for Physics: ");

int physics = scanner.nextInt();

System.out.print("Enter marks for Chemistry: ");

int chemistry = scanner.nextInt();

System.out.print("Enter marks for Maths: ");

int maths = scanner.nextInt();

scanner.close();
// 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

System.out.println("\nAverage Marks: " + average);


System.out.println("Grade: " + grade);

System.out.println("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 java.util.Scanner;

public class PrimeNumberChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input number

System.out.print("Enter a number: ");

int number = scanner.nextInt();

scanner.close();

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) {

System.out.println(number + " is a Prime Number.");

} else {

System.out.println(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 java.util.Scanner;

public class ArmstrongNumberChecker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Get input number

System.out.print("Enter a number: ");

int number = scanner.nextInt();

scanner.close();

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) {

System.out.println(number + " is an Armstrong Number.");

} else {

System.out.println(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 java.util.Scanner;

public class DigitCounter {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Get input number

System.out.print("Enter an integer: ");

int number = scanner.nextInt();

scanner.close();
int count = 0;

int originalNumber = Math.abs(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

System.out.println("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 java.util.Scanner;

public class BMI {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input weight in kg

System.out.print("Enter weight in kg: ");

double weight = scanner.nextDouble();

// Input height in cm

System.out.print("Enter height in cm: ");

double heightCm = scanner.nextDouble();

// Convert height to meters

double heightM = heightCm / 100;

// Calculate BMI

double bmi = weight / (heightM * heightM);

// Output BMI

System.out.printf("Your BMI is: %.2f%n", bmi);


// Determine weight status

if (bmi < 18.5) {

System.out.println("Weight Status: Underweight");

} else if (bmi < 24.9) {

System.out.println("Weight Status: Normal weight");

} else if (bmi < 29.9) {

System.out.println("Weight Status: Overweight");

} else {

System.out.println("Weight Status: Obesity");

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.​

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 java.util.Scanner;

public class AbundantNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input number

System.out.print("Enter an integer: ");

int number = scanner.nextInt();

// 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) {

System.out.println(number + " is an Abundant Number.");

} else {
System.out.println(number + " is Not an Abundant
Number.");

scanner.close();

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 java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

double first, second;

String op;

// Get input values

System.out.print("Enter first number: ");

first = scanner.nextDouble();
System.out.print("Enter second number: ");

second = scanner.nextDouble();

System.out.print("Enter operator (+, -, *, /): ");

op = scanner.next();

// Assuming a number for the loop, for example 1

int number = 1;

for (int i = 1; i < number; i++) {

switch (op) {

case "+":

System.out.println("Result: " + (first +


second));

break;

case "-":

System.out.println("Result: " + (first -


second));

break;

case "*":

System.out.println("Result: " + (first *


second));

break;

case "/":

if (second != 0) {

System.out.println("Result: " + (first /


second));

} 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

You might also like