SAVEETHA SCHOOL OF ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL SCIENCES
AUG -2022
CSA09 Programming in Java
Daily Assessment
1. Write a program using choice to check
Case 1: Given string is palindrome or not
Case 2: Given number is palindrome or not
Sample Input:
Case = 1
String = MADAM
Sample Output:
Palindrome
Test cases:
1. MONEY
2. 5678765
3. MALAY12321ALAM
4. MALAYALAM
5. 1234.4321
#CODE
import java.util.*;
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your choice:");
System.out.println("1. String Palindrome");
System.out.println("2. Number Palindrome");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter a string: ");
String str = sc.nextLine();
String rev = new StringBuilder(str).reverse().toString();
if (str.equals(rev))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
break;
case 2:
System.out.print("Enter a number: ");
int num = sc.nextInt();
int temp = num, revNum = 0;
while (temp > 0) {
revNum = revNum * 10 + temp % 10;
temp /= 10;
}
if (num == revNum)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
break;
default:
System.out.println("Invalid choice!");
}
}
}
2. Find the LCM and GCD of n numbers?
Sample Input:
N value = 2
Number 1 = 16
Number 2 = 20
Sample Output:
LCM = 80
GCD = 4
Test cases:
1. N = 3, {12, 25, 30}
2. N = 2, {52, 25, 63}
3. N = 3, {17, 19, 11}
4. N = -2, {52, 60}
5. N = 2, {30, 45}
#CODE
import java.util.*;
public class LCMGCD {
public static int gcd(int a, int b) {
return b == 0 ? a : 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 sc = new Scanner(System.in);
System.out.print("Enter N value: ");
int n = sc.nextInt();
if (n <= 0) {
System.out.println("Invalid Input");
return;
}
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter number " + (i+1) + ": ");
arr[i] = sc.nextInt();
}
int gcdResult = arr[0];
int lcmResult = arr;
for (int i = 1; i < n; i++) {
gcdResult = gcd(gcdResult, arr[i]);
lcmResult = lcm(lcmResult, arr[i]);
}
System.out.println("GCD = " + gcdResult);
System.out.println("LCM = " + lcmResult);
}
}
3. Write a program to print Right Triangle Star Pattern
Sample Input:: n = 5
Output:
*
**
***
****
*****
#CODE
import java.util.*;
public class RightTrianglePattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
4. Write a program to find the number of student users in the college, get the total users,
staff users details from the client. Note for every 3 staff user there is one Non teaching
staff user assigned by default.
Sample Input:
Total Users: 856
Staff Users: 126
Sample Output:
Student Users: 688
Test Cases:
1. Total User: 0
2. Total User: -143
3. Total User: 1026, Staff User: 1026
4. Total User: 450, Staff User: 540
5. Total User: 600, Staff User: 450
#CODE
import java.util.*;
public class StudentUsers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter total users: ");
int total = sc.nextInt();
System.out.print("Enter staff users: ");
int staff = sc.nextInt();
if (total <= 0 || staff > total || staff < 0) {
System.out.println("Invalid Input");
return;
}
// Non-teaching staff (for every 3 staff one non-teaching)
int nonTeaching = staff / 3;
int students = total - staff - nonTeaching;
if (students < 0) {
System.out.println("Invalid Input");
} else {
System.out.println("Student Users: " + students);
}
}
}