Mohtashim Ali Week 2 - Level 3 - 11 Practice Problems
Mohtashim Ali 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;
System.out.println(isLeapYear(year));
}
if (year % 4 != 0) {
return year + " is not a Leap Year";
} else {
if (year % 100 != 0) {
return year + " is a Leap Year";
} else {
if (year % 400 == 0) {
return year + " is a Leap Year";
} else {
return year + " is not a Leap Year";
}
}
}
}
}
O/P:
O/P:
import java.util.Scanner;
O/P:
Hint =>
A number that can be divided exactly only by itself and 1 are Prime Numbers,
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.
*/
import java.util.Scanner;
System.out.println("Enter a number:");
for(int i=1;i<=n/2;i++)
{ if(n
%i==0)
count++;
if(count>1)
System.out.println("Not Prime");
else
System.out.println("Prime");
}
O/P:
}
if (number == sum) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}
O/P:
O/P:
*/
import java.util.Scanner;
public class BMICalculator { public
static void main(String[] args) {
// Get user input for weight in kilograms
Scanner scanner = new Scanner(System.in);
System.out.print("Enter weight in kg: ");
double weight = scanner.nextDouble();
O/P:
Hint =>
If the number is divisible by the sum, print Harshad Number. Otherwise, print
Not a Harshad Number.
*/
import java.util.Scanner;
System.out.println("Enter a number:");
while(n!=0)
{ n/=10;
sum+=n;
if(n%sum==0)
System.out.println("Harshad number");
else {
O/P:
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;
O/P:
O/P:
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*/
public class DayOfWeek {
public static void main(String[] args) {
// Check if the correct number of command-line arguments are provided
if (args.length != 3) {
System.out.println("Please provide three command-line arguments:
month, day, and year.");
return;
}
// Calculate x
int x = y0 + y0 / 4 - y0 / 100 + y0 / 400;
// Calculate m0
int m0 = month + 12 * ((14 - month) / 12) - 2;
// Calculate d0
int d0 = (day + x + (31 * m0) / 12) % 7;