Q1.
Write a LeapYear program that takes a year as input and outputs the Year is a Leap Year or
not a Leap Year.
//importing modules
import java.util.*;
public class LeapYear{
public static void main(String[] args){
//creating scanner object
Scanner input = new Scanner(System.in);
//intitializing variables
int year;
//taking user input
System.out.println("Enter a year after 1581: ");
year = input.nextInt();
//computing leap year
if (year >= 1582){
if(year%4 == 0 && year%100!=0){
System.out.println("This is a leap year!");
}else if(year%400==0){
System.out.println("This is a leap year!");
}else{
System.out.println("This is not a leap year!");
}
}else{
System.out.println("Invalid Input");
}
//closing scanner object
input.close();
}
}
Q2. Rewrite program 1 to determine Leap Year with single if condition using logical and && and
or || operators
//importing modules
import java.util.*;
public class LeapYear2{
public static void main(String[] args){
//creating scanner object
Scanner input = new Scanner(System.in);
//intitializing variables
int year;
//taking user input
System.out.println("Enter a year after 1581: ");
year = input.nextInt();
//computing leap year
if (year >= 1582){
if((year%4 == 0 && year%100!=0) || year%400 == 0){
System.out.println("This is a leap year!");
}else{
System.out.println("This is not a leap year!");
}
}else{
System.out.println("Invalid Input");
}
//closing scanner object
input.close();
}
}
Q3. 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
//importing modules
import java.util.*;
public class MarksComputation{
public static void main(String[] args){
//creating scanner object
Scanner input = new Scanner(System.in);
//intitializing variables
int marksPhysics, marksChemistry, marksMaths;
double percentage;
//taking user input
System.out.println("Enter your marks in physics: ");
marksPhysics = input.nextInt();
System.out.println("Enter your marks in chemistry: ");
marksChemistry = input.nextInt();
System.out.println("Enter your marks in maths: ");
marksMaths = input.nextInt();
//calculating percentage
percentage = (marksChemistry+marksMaths+marksPhysics)/3.0;
System.out.println("Average Marks: "+percentage);
//computing grade and remark
if(percentage >=80){
System.out.println("Grade: A");
System.out.println("Remark: Level 4, above agency-normalized
standards");
}else if(percentage <80 && percentage>+70){
System.out.println("Grade: B");
System.out.println("Remark: Level 3, at agency-normalized standards");
}else if(percentage <70 && percentage>=60){
System.out.println("Grade: C");
System.out.println("Remark: Level 2, below, but approaching
agency-normalized standards");
}else if(percentage <60 && percentage >=50){
System.out.println("Grade: D");
System.out.println("Remark: Level 1, well below agency-normalized
standards");
}else if(percentage <50 && percentage >= 40){
System.out.println("Grade: E");
System.out.println("Remark: Level 1-, too below agency-normalized
standards");
}else{
System.out.println("Grade: R");
System.out.println("Remark: Remedial standards");
}
//closing scanner object
input.close();
}
}
Q4. Write a Program to check if the given number is a prime number or not
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
//initializing variables
int num;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
// variable to track if the number is prime
boolean isPrime = true;
// Prime numbers are greater than 1
if (num > 1) {
// Loop through numbers from 2 to num
for (int i = 2; i < num; i++) {
if (num % i == 0) { // If remainder is zero, num is not prime
isPrime = false;
break;
}
}
} else {
System.out.println("Number is not natural");
}
// Output result
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
//closing scanner object
input.close();
}
}
Q5. Create a program to check if a number is armstrong or not. Use the hints to show the steps
clearly in the code
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
//initializing variables
int num, numberOfDigits=0,sum=0,originalNum;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
originalNum = num;
//computing number of digits in the number
while(originalNum!=0){
originalNum/=10;
numberOfDigits++;
}
//number of digits is greater than 1
if (numberOfDigits > 1) {
originalNum = num;
//loop through the digits of the number
while(originalNum!=0){
int i =originalNum%10;
sum += (int)Math.pow(i,numberOfDigits);
originalNum/=10;
}
} else {
System.out.println("Invalid Input");
}
// Output result
if (sum == num) {
System.out.println(num + " is an armstrong number.");
} else {
System.out.println(num + " is not an armstrong number.");
}
//closing scanner object
input.close();
}
}
Q6. Create a program to count the number of digits in an integer.
import java.util.Scanner;
public class NumberOfDigits {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
//initializing variables
int num, numberOfDigits=0, originalNum;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
originalNum = num;
//computing number of digits in the number
while(originalNum!=0){
originalNum/=10;
numberOfDigits++;
}
System.out.println("Number of Digiits: "+numberOfDigits);
input.close();
}
}
Q7. 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) {
Scanner scanner = new Scanner(System.in);
//intializing varibales
double weight,height,bmi = 1;
String status;
// Taking user input
System.out.print("Enter your weight in kg: ");
weight = scanner.nextDouble();
System.out.print("Enter your height in m: ");
height = scanner.nextDouble();
// Calculating BMI
bmi = weight / (height * height);
// Determining weight status
if (bmi < 18.5) {
status = "Underweight";
} else if (bmi >= 18.5 && bmi < 24.9) {
status = "Normal weight";
} else if (bmi >= 25 && bmi < 29.9) {
status = "Overweight";
} else {
status = "Obese";
}
// Displaying output
System.out.printf("Your BMI is: %.2f\n", bmi);
System.out.println("Weight status: " + status);
//closing scanner object
scanner.close();
}
}
Q8. Create a program to check if a number taken from the user is a Harshad Number.
import java.util.Scanner;
public class HarshadNumberChecker {
public static void main(String[] args) {
//creating scanner object
Scanner scanner = new Scanner(System.in);
//initializing variable
int num, sum = 0, temp;
// Taking user input
System.out.print("Enter a number: ");
num = scanner.nextInt();
temp = num;
// Calculate the sum of digits
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
// Check if the number is divisible by the sum of its digits
if (num % sum == 0) {
System.out.println(num + " is a Harshad Number.");
} else {
System.out.println(num + " is not a Harshad Number.");
}
//closing scanner object
scanner.close();
}
}
Q9. Create a program to check if a number is an Abundant Number.
import java.util.Scanner;
public class AbundantNumberCheck {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
//initializing variables
int num,sum=0;
//user input
System.out.print("Enter a number: ");
num = input.nextInt();
//numbers are greater than 1
if (num > 1) {
for(int i = 1; i <num;i++){
if(num%i == 0){
sum+=i;
}
}
}
if (sum > num){
System.out.println("It is an abundant number");
}else{
System.out.println("It is not an abundant number");
}
}
}
Q 10. Write a program to create a calculator using switch...case.
import java.util.Scanner;
public class SwitchCalculator {
public static void main(String[] args) {
// creating scanner object
Scanner scanner = new Scanner(System.in);
//taking user input
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter an operator: ");
char operation = scanner.next().charAt(0);
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
//creating calculator using switch case
switch (operation) {
case '+':
System.out.println("Result: " + (num1 + num2));
break;
case '-':
System.out.println("Result: " + (num1 - num2));
break;
case '*':
System.out.println("Result: " + (num1 * num2));
break;
case '/':
if (num2 != 0)
System.out.println("Result: " + (num1 / num2));
else
System.out.println("Error! Division by zero is not allowed.");
break;
default:
System.out.println("Invalid operator!");
}
scanner.close();
}
}
Q11. 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
import java.util.*;
public class DayOfWeek {
public static void main(String[] args) {
//creating scanner object
Scanner input = new Scanner(System.in);
//user input
System.out.println("Enter Month: ");
int m = input.nextInt();
System.out.println("Enter day: ");
int d = input.nextInt();
System.out.println("Enter year: ");
int y = input.nextInt();
//calculating
int y0 = y - (14 - m) / 12;
int x = y0 + y0/4 - y0/100 + y0/ 400;
int m0 = m + 12 * ((14 - m) / 12) - 2;
int d0 = (d + x + (31 * m0) / 12) % 7;
//printing output
System.out.println(d0);
}
}