BHARATH CHOWDRY DS
AAT2
1.Find sum of array elements using recursion
import java.util.Scanner;
public class SumArray {
public static int sumArray(int[] arr, int n) {
if (n <= 0) {
return 0;
return arr[n - 1] + sumArray(arr, n - 1);
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
int sum = sumArray(arr, n);
System.out.println("Sum of array elements: " + sum);
scanner.close();
}
BHARATH CHOWDRY DS
AAT2
2.LCM of two numbers using recursion.
import java.util.Scanner;
public class LCMCalculator {
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = scanner.nextInt();
System.out.print("Enter the second number: ");
int b = scanner.nextInt();
int lcmResult = lcm(a, b);
System.out.println("LCM of " + a + " and " + b + " is: " + lcmResult);
scanner.close();
}
}
BHARATH CHOWDRY DS
AAT2
3.Define a class Employee and Salary Classes with necessary
members and methods. Print the salary slip of an employee.
import java.util.Scanner;
class Employee {
private int id;
private String name;
private String designation;
private Salary salary;
public Employee(int id, String name, String designation, Salary salary) {
this.id = id;
this.name = name;
this.designation = designation;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getDesignation() {
return designation;
}
public Salary getSalary() {
return salary;
BHARATH CHOWDRY DS
AAT2
}
public void printSalarySlip() {
System.out.println("Salary Slip");
System.out.println("-----------");
System.out.println("Employee ID: " + id);
System.out.println("Name: " + name);
System.out.println("Designation: " + designation);
System.out.println(salary.toString());
}}
class Salary {
private double basic;
private double hra;
private double da;
private double deductions;
public Salary(double basic, double hra, double da, double deductions) {
this.basic = basic;
this.hra = hra;
this.da = da;
this.deductions = deductions;
}
public double calculateNetSalary() {
return basic + hra + da - deductions;
}
@Override
public String toString() {
return "Basic Salary: " + basic + "\n" +
BHARATH CHOWDRY DS
AAT2
"HRA: " + hra + "\n" +
"DA: " + da + "\n" +
"Deductions: " + deductions + "\n" +
"Net Salary: " + calculateNetSalary();
}}
public class EmployeeSalarySlip {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Employee ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Employee Name: ");
String name = scanner.nextLine();
System.out.print("Enter Employee Designation: ");
String designation = scanner.nextLine();
System.out.print("Enter Basic Salary: ");
double basic = scanner.nextDouble();
System.out.print("Enter HRA: ");
double hra = scanner.nextDouble();
System.out.print("Enter DA: ");
double da = scanner.nextDouble();
System.out.print("Enter Deductions: ");
double deductions = scanner.nextDouble();
Salary salary = new Salary(basic, hra, da, deductions);
Employee employee = new Employee(id, name, designation, salary);
BHARATH CHOWDRY DS
AAT2
employee.printSalarySlip();
scanner.close();}}
OUTPUT:
BHARATH CHOWDRY DS
AAT2
4.Create class to read time in seconds and convert into time in
(HH:MM:SS) format
import java.util.Scanner;
class TimeConverter {
private int seconds;
public TimeConverter(int seconds) {
this.seconds = seconds;
}
public String convertToHHMMSS() {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
return String.format("%02d:%02d:%02d", hours, minutes, secs);
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
}
BHARATH CHOWDRY DS
AAT2
public class TimeConverterApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
int seconds = scanner.nextInt();
TimeConverter timeConverter = new TimeConverter(seconds);
String timeInHHMMSS = timeConverter.convertToHHMMSS();
System.out.println("Time in HH:MM:SS format: " +
timeInHHMMSS);
scanner.close();
}
}
OUTPUT:
BHARATH CHOWDRY DS
AAT2
5. Check whether the entered character is a vowel or consonant.
import java.util.Scanner;
public class VowelOrConsonant {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
if (isVowel(ch)) {
System.out.println(ch + " is a vowel.");
} else if (Character.isLetter(ch)) {
System.out.println(ch + " is a consonant.");
} else {
System.out.println(ch + " is not a valid alphabet character.");
}
scanner.close();
}
public static boolean isVowel(char ch) {
ch = Character.toLowerCase(ch);
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}
BHARATH CHOWDRY DS
AAT2
6. Check whether entered character is alphabet or not, If yes check
whether uppercase or lowercase
import java.util.Scanner;
public class AlphabetChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
if (Character.isLetter(ch)) {
System.out.println(ch + " is an alphabet character.");
if (Character.isUpperCase(ch)) {
System.out.println(ch + " is an uppercase letter.");
} else {
System.out.println(ch + " is a lowercase letter.");
}
} else {
System.out.println(ch + " is not an alphabet character.");
}
scanner.close();
}
}