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

Mohtashim Ali Week 2 - Level 3 - 11 Practice Problems

The document contains a series of programming exercises focused on Java, including tasks such as determining leap years, calculating student grades, checking for prime and Armstrong numbers, and creating a BMI calculator. Each exercise provides hints and code snippets to guide the user in implementing the solutions. The exercises are designed to enhance programming skills and understanding of fundamental concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views18 pages

Mohtashim Ali Week 2 - Level 3 - 11 Practice Problems

The document contains a series of programming exercises focused on Java, including tasks such as determining leap years, calculating student grades, checking for prime and Armstrong numbers, and creating a BMI calculator. Each exercise provides hints and code snippets to guide the user in implementing the solutions. The exercises are designed to enhance programming skills and understanding of fundamental concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Week 2 - Level 3 - 11 Practice Problems

Name: Mohtashim Ali

Registration No: RA2411033010093

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


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in); System.out.print("Enter
a year: "); int year = scanner.nextInt();

System.out.println(isLeapYear(year));
}

public static String isLeapYear(int year) {


if (year < 1582) {
return "The LeapYear program only works for year >= 1582";
}

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:

2./*Rewrite program 1 to determine Leap Year with single if condition using


logical and && and or || operators.*/ import java.util.Scanner;

public class LeapYear {


public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in); System.out.print("Enter
a year: "); int year = scanner.nextInt();
System.out.println(isLeapYear(year));

public static String isLeapYear(int year) {


if (year < 1582) {
return "The LeapYear program only works for year >= 1582";
}

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


return year + " is a Leap Year";
} else {
return year + " is not a Leap Year";
}
}
}

O/P:

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

import java.util.Scanner;

public class StudentGrades { public


static void main(String[] args) {
// Create a Scanner object to get user input
Scanner scanner = new Scanner(System.in);
// Get input marks for three subjects: Physics, Chemistry, and
Maths System.out.print("Enter marks in Physics: "); int physics
= scanner.nextInt();

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


int chemistry = scanner.nextInt();

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


int maths = scanner.nextInt();

// Compute the total and percentage


int total = physics + chemistry + maths;
double percentage = (total / 300.0) * 100;

// Determine the grade based on the


percentage String grade; if (percentage >=
80) {
grade = "A (Level 4, above agency-normalized standards)";
} else if (percentage >= 70) {
grade = "B (Level 3, at agency-normalized standards)";
} else if (percentage >= 60) {
grade = "C (Level 2, below, but approaching agency-normalized
standards)";
} else if (percentage >= 50) {
grade = "D (Level 1, well below agency-normalized standards)";
} else if (percentage >= 40) {
grade = "E (Level 1-, too below agency-normalized standards)";
} else {
grade = "R (Remedial standards)";
}

// Print the percentage and grade


System.out.printf("Percentage: %.2f%%\n", percentage);
System.out.println("Grade: " + grade);
}
}

O/P:

4. /*Write a Program to check if the given number is a prime number or not

Hint =>

A number that can be divided exactly only by itself and 1 are Prime Numbers,

Prime Numbers checks are done for number greater than 1

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.

Use isPrime boolean variable to store the result

*/

import java.util.Scanner;

public class halo

public static void main(String[] args)


{

Scanner in=new Scanner(System.in);

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

int n=in.nextInt(); int count=0;

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:

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 ArmstrongNumber


{ public static void main(String[]
args) {
Scanner scanner = new
Scanner(System.in); System.out.print("Enter
a number: "); int number = scanner.nextInt();
int sum = 0;
int originalNumber = number; while
(originalNumber != 0) { int remainder
= originalNumber % 10; sum +=
Math.pow(remainder, 3);
originalNumber /= 10;

}
if (number == sum) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}

O/P:

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


public static void main(String[] args) {
// Get integer input from the user
Scanner scanner = new
Scanner(System.in); System.out.print("Enter
a number: "); int number = scanner.nextInt();

// Create an integer variable count with value 0


int count = 0;

// Use a loop to iterate until number is not equal to 0


while (number != 0) {
// Remove the last digit from number in each iteration
number /= 10;
// Increase count by 1 in each iteration
count++;
}

// Display the count to show the number of digits


System.out.println("Number of digits: " + count);
}
}

O/P:

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 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();

// Get user input for height in centimeters


System.out.print("Enter height in cm: ");
double heightInCm = scanner.nextDouble();

// Convert height from cm to meters


double heightInMeters = heightInCm / 100;

// Calculate BMI using the formula


double bmi = weight / (heightInMeters * heightInMeters);

// Display the BMI value


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

// Determine the weight status based on the BMI value


if (bmi < 18.5) {
System.out.println("You are underweight.");
} else if (bmi >= 18.5 && bmi < 24.9) {
System.out.println("You are of normal weight.");
} else if (bmi >= 25 && bmi < 29.9) {
System.out.println("You are overweight.");
} else {
System.out.println("You are obese.");
}
}
}

O/P:

8. /*Create a program to check if a number taken from the user is a Harshad


Number.

Hint =>

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

Get an integer input for the number variable.

Create an integer variable sum with initial value 0.

Create a while loop to access each digit of the number.

Inside the loop, add each digit of the number to sum.


Check if the number is perfectly divisible by the sum.

If the number is divisible by the sum, print Harshad Number. Otherwise, print
Not a Harshad Number.

*/

import java.util.Scanner;

public class w2L2

public static void main(String[] args)


{

Scanner sc=new Scanner(System.in);

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

int n=sc.nextInt(); int sum=0;

while(n!=0)

{ n/=10;

sum+=n;

if(n%sum==0)

System.out.println("Harshad number");

else {

System.out.println("Not Harshad number");

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;

public class AbundantNumber


{ public static void main(String[]
args) {
// Get an integer input for the number variable
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();

// Create an integer variable sum with initial value 0


int sum = 0;

// Run a for loop from i = 1 to i < number


for (int i = 1; i < number; i++) {
// Inside the loop, check if number is divisible by
i if (number % i == 0) { // If true, add i
to sum sum += i;
}
}

// Outside the loop, check if sum is greater than number


if (sum > number) {
System.out.println(number + " is an Abundant Number.");
} else {
System.out.println(number + " is not an Abundant Number.");
}
}
}

O/P:

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) {
// Get user input for the two double variables named first and second
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
double first = scanner.nextDouble();
System.out.print("Enter the second number: ");
double second = scanner.nextDouble();

// Get user input for the String variable named op


System.out.print("Enter the operator (+, -, *, /): ");
String op = scanner.next();

// Perform specific operations using switch...case based on the


input value of op 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 "/":
// Check for division by zero
if (second != 0) {
System.out.println("Result: " + (first / second));
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid Operator");
break;
}
}
}

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;
}

// Parse command-line arguments


int month = Integer.parseInt(args[0]);
int day = Integer.parseInt(args[1]);
int year = Integer.parseInt(args[2]);
// Calculate y0
int y0 = year - (14 - month) / 12;

// 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;

// Print the day of the week


System.out.println("The day of the week is: " + d0);

// Print the day of the week in words (Optional)


String[] daysOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
System.out.println("Which is: " + daysOfWeek[d0]);
}
}
O/P:

You might also like